import so file from different folder - python-3.x

I am using ubuntu 20.04 and python3. I want to import so file "ext.so" like this:
impot Ext
from another code. But the so file is in different folder. What is the right way to do it?

What is the right way to do it?
your project should be structured like so:
-head
--sub1
---Ext.so
--sub2
---caller.py
you should have head the folder containing head in your pythonpath somehow (by installing the python module using distutils, or just having head as your working directory or added by modifying PYTHONPATH in .bashrc, or appending it to sys.path in your script), and you should use
from head.sub1 import Ext
granted that your .so file is a python extension and not some sort of dll, anyone installing your project should be able to run your code without any problems.
however, there is definitely nothing stopping you from adding sub1 to your pythonpath and just import Ext.
Edit: i am sorry, if head is in pythonpath, you only need to import from sub1, not head, so you should have the folder containing head in your pythonpath, my bad.

Related

Pycharm unresolved reference highlighting

I'm having difficulties understanding how pyCharm resolves references
I have a project structure like this:
ProjectFolder/
main.py
libraries/
my_library.py
sublibraries/
my_sublibrary.py
Basically libraries is a folder that contains some files (libraries) and some subfolders for better structuring of the libraries
main.py imports from libraries ans sublibraries without any problem.
my_library.py imports some classes from sublibraries
# inside my_library.py
from sublibraries.my_sublibrary import MyClass
However pycharm highlights sublibraries in red with the error
Unresolved reference 'sublibraries'
This doesn't really affect the execution of the program. It works fine, but the red highlightting is annoying.
I understand that pycharm has ProjectFolder as the current folder and it cannot find subfolders since there's a folder in the middle.
I tried a couple of things:
I can solve this by mark directory as sources root but I'm not sure what does this do, and since I'm not commiting .idea files, whoever pulls the repository has the same hightlighting problem and has to manually do the same.
I also tried doing a relative import
# inside my_library.py
from .sublibraries.my_sublibrary import MyClass
This solves the highlighting problem, but the program then gives an error:
ModuleNotFoundError: No module named '__main__.sublibraries'; '__main__' is not a package
I'm not sure what is the best approach to have such a simple file structure working, be able to distribute it via git and have correct hightlighting.
Should I commit .idea files so the others can import pycharm settings and use the Mark as Sources Root option?
You can create empty __init__.py files in the libraries/ and sublibraries/ folders, which will turn these folders into (sub)packages. This will make the relative import work (and remove the highlighting).

How to manually import python modules from site-packages directory?

I have to present my application on another PC, which does not have the modules that I use already installed so I have to locally import everty pythom module that I use. I use a lot of them, like: OpenCV, TesorFlow, Keras, Kivy, sci-kit-image, etc. I discovered that all my python packages are installed in the directory site-packages, which is a part of the tree where is python installed. I want to copy-paste my site-packages directory to the other PC and import all that I need when my app has to run on the other PC. How do I set all modules from site-packages locally to be available for import in my app?
The second solution I could think of would be to download all the modules from the git and official sites, and import them into my app, but it's hard labour and I have to write a .bat file to install them... but I'm pretty sure I don't have the permission to install anything...
Is there a way for my first solution? pls I really need to know
Edit: I use python 3.6
If you copy the whole package directory to your project directory, it should work. It then will be imported as though it is your own module.
Look at this: How does python find a module file if the import statement only contains the filename?

Copy non python files via package_data to Scripts directory

I have some scripts in my package, that rely on some template xml files.
Those scripts are callable by entry points and I wanted to reference the template files by a relative path.
When calling the script via python -m ... the scripts themselves are called from within lib\site-packages and there the xml files are available as I put them in my setup.py like this:
setup(
...
packages=['my_pck'],
package_dir={'my_pck': 'python/src/my_pck'},
package_data={'my_pck': ['reports/templates/*.xml']},
...
)
I know, I could copy those templates also by using data_files in my setup.py but using package_data seems better to me.
Unfortunately package_data seems not to copy those files to the Scripts folder where the entry points are located.
So my question is, is this even achievable via package_data and if, how?
Or is there a more pythonic, easier way to achieve this? Maybe not referencing those files via paths relative to the scripts?
Looks like importlib-resources might help here. This library is able to find the actual path to a resource file packaged as package_data by setuptools.
Access the package_data files from your code with something like this:
with importlib_resources.path('my_pck.reports.templates', 'a.xml') as xml_path:
do_something(xml_path)

Python pkg_resources and file access in packages

I'm building my first python package (which I then install with pip) and I need to use some non-python files. In these answers, it is explained that I should use the pkg_resources function. But I can't figure out a working example. Let say I have this project structure:
package_name/
----data/
--------image.png
----package_name/
--------__init__.py
--------file.py
----setup.py
----MANIFEST.in
----conf.yml
Now I want to access conf.yml and image.png from file.py. How should I proceed in:
file.py ?
setup.py ?
MANIFEST.in ?
The simplest way to access these files would be to include in MANIFEST.in
global-include *.png
global-include *.yml
MANIFEST.in will only use files for a source distribution though, while setup.py will include files for binary/wheel distributions so just to be safe, inside of your setup.py
include_package_data = True,
package_data = {
'' : ['*.png'],
'' : ['*.yml'],
}
Then you can reference the specific file like so from file.py
from pkg_resources import resource_string
def foo():
pngfile = resource_string(__name__, 'data/image.png')
ymlfile = resource_string(__name__, 'conf.yml')
Notice how for the png file I've specified the directory.
This solution also does not account for files of the same extension which you may want to exclude, but those could easily be taken care of with exclude or specifying filenames rather than using the asterisk.
I know there are questions that could easily be considered duplicates, but I had trouble getting a workable example as well and after a good while badgering away myself I managed to get something to work, and this was it.

Packaging Multiple Python Files

I currently am using this guide to package up my project wasp. However currently everything lives inside of the wasp file.
That's not ideal. I would rather have all the classes in separate files so it can be more effectively managed. I have the series of files needed in the debian directory. But I'm not sure how to configure the packaging to package multiple files.
Is there a way to change my packaging to package more than just the one script file?
I'm not a debian package or Python expert, but one way would be to copy the various source files to another location (outside of /usr/bin), and then have /usr/bin/wasp call out to them.
Say you put all of your python code in src/ in the root of your repo. In the debian/install file, you'd have:
wasp usr/bin
src/* usr/lib/wasp/
You'd then just need /usr/bin/wasp to call some entry point in src. For example,
#!/usr/bin/python3
import sys
sys.path.append('/usr/lib/wasp/')
import wasp # or whatever you expose in src
# ...
Again, I don't know the best practices here (either in directory or python usage) but I think this would at least work!

Resources