How to force install package in virtualenv?

petriichuk picture petriichuk · Oct 31, 2018 · Viewed 7.1k times · Source

Trying to install django with different version that in system, it shows me:

Installing collected packages: Django
Found existing installation: Django 1.7.11
Not uninstalling django at /home/user/lib/python2.7, outside environment /home/user/webapps/v2_dev/venv

Successfully installed Django-1.8.19

But in fact there is old version

tried with different commands:

./venv/bin/pip install Django==1.8.11

pip install Django==1.8.11

UPDATED: When I install my packages it shows:

The required version of setuptools (>=16.0) is not available,
and can't be installed while this script is running. Please
install a more recent version first, using
'easy_install -U setuptools'.
(Currently using setuptools 3.1 (/home/user/lib/python2.7/setuptools-3.1-py2.7.egg))

When I do the upgrade:

venv/bin/pip install --upgrade setuptools
Requirement already up-to-date: setuptools in ./venv/lib/python2.7/site-packages (40.5.0)

Answer

machnic picture machnic · Oct 31, 2018

Instead of installing setuptools and Django like ./venv/bin/pip install ..., try to activate your virtual environment first and install the stuff you need afterwards.

Activating virtual environment:

Go to the folder where your virtual environment is located (typically the root folder of your project) and type one of the two:

  • source venv/bin/activate (Unix-based systems)
  • venv\Scripts\activate (Windows)

This will ensure that you are not mixing packages installed in different environments.

Forcing reinstall of the packages:

  • Simple upgrade can be done by adding: --upgrade or -U
  • Forcing reinstall of the packages can be done by adding: --force-reinstall

In your case (once the environment is activated):

python -m pip install -U --force-reinstall setuptools Django

Step by step:

  1. Deactivate and delete the old virtual environment
  2. Create new environment using python -m virtualenv venv (python 2) or python -m venv venv (python 3)

    python above is the interpreter which you want to use in your project. That's the only point where you might want to use for example python3 or some absolute path instead. Later use the code as is.

  3. source venv/bin/activate

    Activating the virtual environment

  4. python -m pip install -U pip

    If you have issue with ImportError: No module named _internal than probably you are using an old version of pip. Issue is described here

  5. python -m pip install -U --force-reinstall -r requirements.txt

    -U --force-reinstall is a bit of an overkill in case of fresh environment, but it will do no harm

  6. Go to the place where your manage.py is located and start the server using python manage.py runserver