0

I had a project set up within VS-Code together with a venv (setup via "python -m venv venv") in it. Everything was working fine: Git worked and my venv did also do its job, e.g. opening python via the terminal while the venv was active would open the right python interpreter within the venv and thus find the correct libraries.

I decided to open a new branch via the git command : "git checkout -b eel_replacement" and wanted to install Django afterwards via "pip install django"(while my venv was active). Installation succeded and I wanted to verify that by opening the python interpreter and trying "import django"(I also tried "import Django"), which did not work, resulting in the error:

Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'django'

If I type in pip list, then this is the output: pip list output

There are 2 things that really got me confused right here: It does show all packages, not only the ones I installed within my venv. This normally indicates that my venv is not active! Nonetheless you can see it still shows the "(venv)" line before my command prompt, which normally indicates that venv is activated.

Furthermore the "pip list" output shown above also lists Django being installed succesfully. So i assumed that the venv was running but the django package was unintendedly installed on my main machine, which resulted in me being unable to import it from within my venv. So i tried this way to see whether the venv was active or not. Weirdly enough this returns a True(as shown here), meaning that my venv is not active at all, which left me entirely clueless.

I've got another Project in which I use git and github together with a venv which still works perfectly fine. This is the output of the sys.prefix==sys.base_prefix code within that other project, which returns a False as expected. Checking out back and forth between master and eel_replacement branch did not solve the issue. Neither did restarting/reopening the project.

I am uncertain whether that is some kind of very weird bug, given the inconsistency of hints concerning venv being active or not OR its just me overseeing something obvious. I would defenitly appreciate some help. Also this is my first question on stackoverflow, so be gentle with me^^

nick
  • 1
  • Python virtual environments work by setting various "path" variables in your environment (hence the name virtual *environment*). This changes *where* Python and its pieces look for files. Git, meanwhile, works by having a repository somewhere, and—typically—using `git checkout` to *extract* files to some directory. Without knowing more about your particular Git repository, it seems likely that you have set up your venv to look for files where Git put them ... and then you told Git to take those files away, and put other files in instead. So the ones Python needs are gone now. – torek Mar 28 '21 at 01:24
  • When combining Git with venv, it's important to make sure that either: (1) the repository's extracted files have nothing to do with where Python is looking, or (2) the extracted files *always* include *all* the files needed, so that Python doesn't have to look elsewhere. (It *is* possible to mix-and-match but this gets tricky and I would advise against it if possible.) – torek Mar 28 '21 at 01:26

0 Answers0