I use virtual environments in my django projects. When I create my venv, I do like this from my command line
cd Dev python3 -m venv <name of venv>
This creates a folder called venv on my mac machine in my Dev folder.
After I pip install django in the venv, I use the pip list
command. At this point the list only contains the default django packages. It looks like this for example:
asgiref 3.5.2 Django 4.0.6 pip 22.0.4 setuptools 58.1.0 sqlparse 0.4.2
Then I start my django project like this.
django-admin startproject <name of project>
After that when I move the venv folder inside my main project folder, and run pip list
again, all dependencies from previous projects are being added to the list. Now it looks like this
asgiref 3.4.1 boto3 1.24.12 botocore 1.27.12 certifi 2022.5.18.1 charset-normalizer 2.0.12 Django 4.0.5 django-cors-headers 3.13.0 django-dotenv 1.4.2 django-embed-video 1.4.4 django-filter 21.1 django-htmx 1.9.0 django-js-asset 2.0.0 django-mathfilters 1.0.0 django-storages 1.12.3 djangorestframework 3.13.1 gunicorn 20.1.0 idna 3.3 jmespath 1.0.1 Pillow 9.1.1 pip 22.1.2 psycopg2 2.9.3 psycopg2-binary 2.9.3 python-dateutil 2.8.2 pytz 2022.1 requests 2.28.0 s3transfer 0.6.0 setuptools 58.1.0 six 1.16.0 sqlparse 0.4.2 urllib3 1.26.9
I can’t figure out why this is happening.
Advertisement
Answer
Don’t move your virtual environment after creation. See this question for motivation and explanation. You can install Django on system level and create environment only when project directory already exists to avoid this moving.
And here’s why your environment stops working after moving. Suppose (/
for simplicity) you have /myenv
environment path, then after . activate
your path looks like
PATH="/myenv/bin:<other entries>:/usr/bin:<...>"
Now python
resolves to (which python
) /myenv/bin/python
.
Then you move your environment. PATH
remains unchanged, but now there is no /myenv/bin
directory, so python
resolves to /usr/bin/python
and pip
to /usr/bin/pip
. The fact that now pip
has many packages available means that you have installed dependencies for your previous project system-wide (maybe accidentally, forgotten to activate venv). You can confirm it using pip
without active venv.