Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 910
Taras Avatar asked Oct 31 '18 07:10

Taras


2 Answers

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.

like image 106
Martino Avatar answered Nov 15 '22 03:11

Martino


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

like image 20
machnic Avatar answered Nov 15 '22 04:11

machnic