[Django]-Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x

1👍

✅

You’ll definitely want to use a virtualenv to separate your Django project from the system Python version. virtualenv creates a virtual environment – a dedicated version of Python just for your Django project – so you don’t install any libraries with the system Python version, which is used for many other things. virtualenvwrapper makes working with virtualenv easy. pip install virtualenvwrapper is your friend. Here’s how to install:

# Install virtualenvwrapper with the system Python version
pip install virtualenvwrapper
# Then, add these lines to your .bashrc, with the appropriate path to Python 3
# You can find the path to Python 3 with "which python3"
# virtualenvwrapper.sh is typically in /usr/bin or /usr/local/bin 
export VIRTUALENV_PYTHON=/usr/local/bin/python3.5
source /usr/bin/virtualenvwrapper.sh

After that initial install, here’s how to work with virtualenvwrapper:

# Make a virtualenv
mkvirtualenv my_project

# You're now in the environment for your project
pip install Django

# When you need to activate the virtualenv to work on your project
workon my_project

# Show the virtualenvs you've created - I use one for each Django or Python project I create
lsvirtualenv

Much more here: https://virtualenvwrapper.readthedocs.io/en/latest/

Good luck!

4👍

You shouldn’t have site-packages in your PYTHONPATH at all; it’s already going to be in sys.path for the appropriate version of Python, but having it in PYTHONPATH mean the other version of Python will try to use the incompatible modules found there. Figure out where you are setting PYTHONPATH to that (it shouldn’t happen in any default setup, so you probably did the wrong thing in ~/.bashrc or the like manually), and stop doing it. If you can’t figure it out, unset PYTHONPATH will remove it.

Since you want to use Python 3, use pip3 for package management, not pip.

Leave a comment