Taras
Taras

Reputation: 507

How to force install package in virtualenv?

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)

Upvotes: 6

Views: 18943

Answers (4)

Gilson Souza
Gilson Souza

Reputation: 33

workaround but it works!

in your virtualenv directory change the properties of the pyvenv.cfg file

include-system-site-packages = True

this will cause the packages installed on the main to be used

Upvotes: 1

Martino
Martino

Reputation: 768

I arrived at this post while looking for how to force install something in a virtualenv despite it being already installed in the global python. This happens when the virtual env was created with --system-site-packages.

In this situation, for certain packages it may be important to have a local version within the virtualenv, even if for many other packages we can share the global versions. This is the case of pytest, for example. However, pip will refuse to install a package in the virtualenv if it can already find the most recent version in the system site.

The solution is to use pip install --ignore-installed mypackage.

Upvotes: 21

Taras
Taras

Reputation: 507

The problem was in Webfaction VPS

Need an empty file named sitecustomize.py in the /home/username/webapps/appName/env/lib/python2.

That empty file overrides their python customizations, one of which is to include any packages in the ~/lib/python2.7 directory.

You might need to deactivate your virtual env and activate it again for changes to take effect.

Upvotes: 0

Michał Machnicki
Michał Machnicki

Reputation: 2887

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

Upvotes: 7

Related Questions