Skip to content Skip to sidebar Skip to footer

Can't Import Module From Bin Directory Of The Same Project

I'm building a library that will be included by other projects via pip. I have the following directories ('venv' is a virtualenv): project \- bin \- run.py \- myproj

Solution 1:

Install myproject into venv virtualenv; then you'll be able to import myproject from any script (including bin/run.py) while the environment is activated without sys.path hacks.

To install, create project/setup.py for the myproject package and run from the project directory while the virtualenv is active:

$ pip install -e .

It will install myproject inplace (the changes in myproject modules are visible immediately without reinstalling myproject).


Solution 2:

The solution here is to source the virtualenv you have and then install the package in developer mode.

source venv/bin/activate

pip install -e .

You can then import myproject.logger from run.py.

You'll need to create a setup.py file as well to be able to install the package into your environment. If you don't already have one you can read the official documentation here.


Solution 3:

Only the current working directory is inside the PYTHONPATH, which is used to resolved dependencies. So, if you are inside bin and execute your script, project is not in the path anymore. You have to use one of the common methods to add project to the PYTHONPATH, either by appending to the environment variable or through editing the sys.path list programmatically, as indicated in the other answer.


Solution 4:

add the path of project in the PYTHONPATH


Post a Comment for "Can't Import Module From Bin Directory Of The Same Project"