Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python venv ModuleNotFoundError

(Before marking me with duplicate question votes, please note that everything that I can find on this question has to do with virtualenv, not venv)

System:

  • Ubuntu Server 16.04
  • Python 3.6 installed, Python 3.5 native

Background (Contrived example, so might have a typo or two, but the idea is the important part)

I have a project in the form of:

Project/
├── __init__.py
└── project
    ├── packageA
    │   ├── fileA.py
    │   └── __init__.py
    └── packageB
        ├── fileB.py
        └── __init__.py

in fileb.py, I have an import statement such as

import project.packageA.fileA

I create a venv by;

cd /path/to/Project; python3.6 -m venv .venv; source .venv/bin/activate

then I run

source project/packageB/fileB.py

This will give me an error:

ModuleNotFoundError: no module named 'project'

Attempts to address:

  1. Add the path to "Project" and "project" to $PATH
  2. Modify $PYTHONPATH in the .venv/bin/activate script so that sys.path now points to "Project" and "project"
  3. Completely read the docs here
  4. Verified that os.sys.path shows Project and project paths

Question: It must be possible to do module imports using venv or it would be of zero value -- so what am I fundamentally missing in my setup? (With viritualenv, I just used 'add2virtualenv')

[Edit - Showing more detail]

# Changes to .venv/bin/activate
PYTHONPATH="/home/steve/Temp/Project:/home/steve/Temp/Project/project:$PYTHONPATH"
export PYTHONPATH

python -c "import os; print(os.sys.path)"
['', '/home/steve/Temp/Project', '/home/steve/Temp/Project/project', .....

[Edit2 - adding packageA to PYTHONPATH Works]

If I add 'path/to/packageA' to my PYTHONPATH, the import works. To use this, I would have to add each subpackage to my project -- less than ideal for large projects.

like image 945
SteveJ Avatar asked Sep 20 '25 19:09

SteveJ


1 Answers

This line

​$ source project/packageB/fileB.py

fails because

  1. the import path is messed up, it includes the project folder but it should not
  2. project is possibly not in your PYTHONPATH

To fix it

Step 1) fix the import statement in fileB.py, replace your import with

​import packageA.fileA

Step 2) Confirm for yourself whether you added project to PYTHONPATH by checking your bash environment

​$ echo $PYTHONPATH     # does it contain `path/to/project`?

If not temporarily fix it

​$ export PYTHONPATH=path/to/project:$PYTHONPATH   # forget `/path/to/Project` you only need `path/to/Project/project`

(Note changes to $PATH are irrelevant to Python package/module searches, so that was a wasted attempt).

Then when you run your script, it will not fail:

$ source project/packageB/fileB.py      # success?!

By the way it is better to call your python scripts with python:

$ python project/packageB/fileB.py

Finally, permanently update your virtual environment by editing the activate script in your virtual environment's bin directory. Add the PYTHONPATH export above somewhere near the top.

like image 150
Riaz Rizvi Avatar answered Sep 22 '25 08:09

Riaz Rizvi