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?

6
  • you are saying you are using Windows server but you have described virtualenv procedure of Linux based system Commented Jun 24, 2019 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 Commented Jun 24, 2019 at 17:51
  • Does Django folder avalible under Scripts/site-packages? Django folder must be present within venv site packages. Commented Jun 24, 2019 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) Commented Jun 24, 2019 at 18:17
  • 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 Commented Jun 24, 2019 at 18:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.