Reputation: 663
I have a python 3 package with the following structure:
.
├── package
│ └── bin
└── main_module
│ └── lib
│ ├── __init__.py
│ ├── module1.py
│ ├── module2.py
│ └── module3.py
│ └── test
│ ├── test1.py
│ ├── test2.py
│ └── test3.py
│ └── setup.py
Usually, one runs $ python3 setup.py install
and all is good. However, I want to use this package on a cluster server, where I don't have write permissions for /usr/lib/
. The following solutions came to my mind.
Somehow install the package locally in my user folder.
Modify the package such that it runs without installation.
Ask the IT guys to install the package for me.
I want to avoid 3., so my question is whether 1. is possible and if not, how I have to modify the code (particularly the imports) in order to be able to use the package without installation. I have been reading about relative imports in python all morning and I am now even more confused than before. I added __init__.py
to package and bin and from what I read I assumed it has to be from package.lib import module1
, but I always get ImportError: No module named lib
.
Upvotes: 7
Views: 17595
Reputation: 3609
you can add this to the "main file" of the package
import sys, os
sys.path.append(os.path.dirname(__file__) + "/..")
you can find the "main file" by looking for this pattern
if __name__ == "__main__":
some_function()
Upvotes: 1
Reputation: 403
I had the same problem. I used the first approach
install the package locally in my user folder by running
python setup.py install --user
This will install your module in ~/.local/lib/python3/
Upvotes: 2
Reputation: 107347
In order for Python to be able to find your modules you need to add the path of your package to sys.path
list. As a general way you can use following snippet:
from sys import path as syspath
from os import path as ospath
syspath.append(ospath.join(ospath.expanduser("~"), 'package_path_from_home'))
os.path.expanduser("~")
will give you the path of the home directory and you can join it with the path of your package using os.path.join
and then append the final path to sys.path
.
If package
is in home directory you can just add the following at the leading of your python code that is supposed to use this package:
syspath.append(ospath.join(ospath.expanduser("~"), 'package'))
Also make sure that you have an __init__.py
in all your modules.
Upvotes: 6
Reputation: 990
Just add the path of your 'package' to environment variable PYTHONPATH
. This will get rid of the error you are getting.
OR
programmatically add path of the package to sys.path.append()
Upvotes: 1