4

My virtual environment refuses to recognize my install of Django (strangely)

I'm on Windows Server, installed Python 3.7 to a directory (C:\Python37) which I have C:\Python37;C:\Python37\Scripts in my windows Path so when using Powershell or GitBash I can use the python command.

if I run where python it shows the default install

I CD into my django project directory and run: python virtualenv venv and the venv directory is created

Then I run source venv/Scripts/activate and it activates appropriately.

When I run where python it shows the exe inside the venv directory - which is expected and appropriate.

I run pip install -r requirements.txt and all my requirements install appropriately. I confirm they are installed with pip freeze (all installed correctly)

Once I do that I go to run python manage.py collectstatic (no migrations are required in this particular instance) I get an error message that Django isn't installed.

To check this, with my virtualenv still activated I enter the shell (python)

If I do import django it also says Django is not installed.

I cannot figure out what's happening here - the python version appears to be correct, the correct virtualenv is activated - but it's still not seeing the properly installed Django installation.

Thoughts? Ideas?

Hanny
  • 580
  • 3
  • 16
  • 44
  • you are saying you are using Windows server but you have described virtualenv procedure of Linux based system – Vardhman Patil Jun 24 '19 at 17:49
  • Once python is installed and the path variables are all set - it's the same process. Once you `pip install virtualenv` the process is the same except on windows you have to specify `source venv/Scripts/activate` whereas on Linux it is `source venv/bin/activate` – Hanny Jun 24 '19 at 17:51
  • Does Django folder avalible under Scripts/site-packages? Django folder must be present within venv site packages. – Vardhman Patil Jun 24 '19 at 17:57
  • Django installs as expected (`venv/Lib/site-packages`) - the Scripts directory is only used to activate the virtualenv (same as the `bin` in *nix) – Hanny Jun 24 '19 at 18:17
  • Trying to reproduce the error but in my case no error. Only difference is that I have activated environment with venv/Scripts/activate. No source infront of the command. I am understanding the meaning of source in Windows. – Vardhman Patil Jun 24 '19 at 18:19
  • 1
    Figured it out - so when I would run `python manage.py collectstatic` instead of using the activated virtualenvironment, it uses the Python installation that was explicitly stated in `.bashrc` (using an alias apparently) for this particular user account. Ugh. Thank you for your help @ANDY_VAR – Hanny Jun 24 '19 at 18:22

1 Answers1

2

For what it's worth - here is the solution and explanation:

Everything works as expected as was outlined in the question - the reason that the actual python command wasn't working had to do with a line in the .bashrc file.

There was an alias in the .bashrc file to set python to the command winpty C:\Python37\python

So when the command python manage.py collectstatic was getting ran - it was looking at the Python executable in the Python37 directory and not the virtualenvironment Python.

This was solved by simply running the appropriate Python (e.g.) C:/my_project/venv/Scripts/python manage.py collectstatic

This forced it to use the virtualenvironment python to run the command so everything worked as expected.

Hanny
  • 580
  • 3
  • 16
  • 44