0

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.

RandomMadi
  • 87
  • 2
  • 10
  • 2
    Show us the exact commands to reproduce incl. the relevant output! – Klaus D. Jul 17 '22 at 10:22
  • Please show how you are creating the virtual environment... – Willem Van Onsem Jul 17 '22 at 10:28
  • I have added it now. – RandomMadi Jul 17 '22 at 10:35
  • `when I move the venv folder inside my main project folder` [You should never do it](https://stackoverflow.com/questions/32407365/can-i-move-a-virtualenv), it should be the source of problem (although I don't understand the reason of this odd `list` behavior). Probably your previous project venv is on PATH somehow. – STerliakov Jul 17 '22 at 11:22
  • @SUTerliakov, then where should I store the venv? – RandomMadi Jul 17 '22 at 12:44
  • Personally I have Django installed in system site-packages (as well as linters - black, pre-commit, isort) and create venv only after project creation. If you for some reason extremely dislike such way or need outdated Django version - create venv, install Django, remove venv, create new venv in new directory, install again. But this is an opinion-based question now. – STerliakov Jul 17 '22 at 13:25
  • And to answer your question on odd `list` output, could you check `which python` before and after venv moving and show your `PATH`? – STerliakov Jul 17 '22 at 13:26
  • Before the path is `/Dev/venv/bin/python` and after it is `/usr/bin/python`. I have also noticed that if I run `pip list` outside any venv, I also get the long list. – RandomMadi Jul 17 '22 at 13:34

1 Answers1

1

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.

STerliakov
  • 4,983
  • 3
  • 15
  • 37