Tjm92
Tjm92

Reputation: 317

Struggling to get Django server to run in virtual environment

I'm new to using Django & Python and am having some difficulty trying to get my manage.py application to run in a virtual environment.

I am receiving the following errors at the top of my models.py file:

I initially thought that this was an issue with pylint not pointing at the right environment (when I switch my Python interpretter to the one outside of the virtual environment, the pylint errors disappear); however, whenever I try to run my server, I get the following error:

(.venv) tjmcerlean@LAPTOP-RKLCBQLO:/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce$ python manage.py runserverTraceback (most recent call last):
  File "manage.py", line 10, in main
    from django.core.management import execute_from_command_line
  File "/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce/.venv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
    from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 12, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

This error appears to suggest that the virtual environment is not activated. However, I activated it using the location of the virtual environment (.venv/bin/activate) and saw that the bash terminal now displays (.venv):

Additionally, when using the (pip freeze) command, I can see that Django is installed within the virtual environment too:

I'm lost as to why the server won't run and would really appreciate any advice.

Thank you!

Upvotes: 0

Views: 689

Answers (2)

Unknown Beginner
Unknown Beginner

Reputation: 193

The problems occur when you don't include a path in virtualenv.

Try adding the path of python on your vscode. Or, try to install and uninstall the Django projects.

Upvotes: 0

frost-nzcr4
frost-nzcr4

Reputation: 1620

Calling django.__file__ in activated env reveals the real path:

python -c "import django; print(django.__file__)"

it should looks like /usr/local/lib/python3.8/site-packages/django/__init__.py then re-check PYTHONPATH environment variable.

Maybe you've installed 2 Django in venv and system-wide but in some of them there is no db. And also remove all of *.pyc files in your working folder.

Upvotes: 0

Related Questions