Issue
I created a django project, set up a virtual environment, and added django with poetry add
.
inside pyproject.toml:
[tool.poetry.dependencies]
python = "^3.9"
psycopg2-binary = "^2.9.3"
Django = "^4.0.1"
Inside venv I run poetry show
:
asgiref 3.5.0 ASGI specs, helper code, and adapters
django 4.0.1 A high-level Python web framework that encourages rapid development and clean, pragmatic design.
psycopg2-binary 2.9.3 psycopg2 - Python-PostgreSQL Database Adapter
sqlparse 0.4.2 A non-validating SQL parser.
When I run command to create an app:
p manage.py startapp users apps/users
I get this error:
(base) ┌──(venv)─(tesla㉿kali)-[~/Documents/projects/graphql/graphenee]
└─$ p 1 ⨯
Python 3.9.7 (default, Sep 16 2021, 13:09:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
>>>
venv is set, activated, django is installed but I am still getting this error. Inside virtual envrionment I start python shell and import django:
Python 3.9.7 (default, Sep 16 2021, 13:09:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'django'
Django is also globally installed and when I start the python shell in global environment, I can import django:
Python 3.9.7 (default, Sep 16 2021, 13:09:58)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>>
Solution
It seems that you have manually created a virtual env in the project directory by e.g. python -m venv venv
. So now you have one in /home/tesla/Documents/projects/graphql/graphenee/venv/
.
After that you added some packages with poetry. However, by default poetry will only look for .venv
directory (note the starting dot) in the project directory. Since poetry did not find a .venv
, it created a new virtual env in /home/tesla/.cache/pypoetry/virtualenvs/graphenee-CXeG5cZ_-py3.9
and installed the packages you added via poetry add
there.
The problem is that you try to use the "empty" virtual env in the project directory instead of the one created by poetry. Fortunately with poetry it is very easy to run command, even without activating the venv, just use poetry run
in the project directory.
To check Django installation:
poetry run python
# Executes: /home/tesla/.cache/pypoetry/virtualenvs/graphenee-CXeG5cZ_-py3.9/bin/python
>>> import django
To run Django management commands:
poetry run ./manage.py startapp users apps/users
It will use the virtual env in /home/tesla/.cache/pypoetry/virtualenvs/graphenee-CXeG5cZ_-py3.9
. You can delete venv
in the project directory.
Note: if you rather want to use a virtual env in the project directory, then delete /home/tesla/.cache/pypoetry/virtualenvs/graphenee-CXeG5cZ_-py3.9
, then create one in the project directory by
python -m venv .venv`
After that install packages with poetry:
poetry install
Now poetry will use the local virtual env in /home/tesla/Documents/projects/graphql/graphenee/.venv
when you run a command via poetry run [cmd]
.
Answered By - Dauros Answer Checked By - Candace Johnson (WPSolving Volunteer)