5

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)
machnic
  • 2,304
  • 2
  • 17
  • 21
Taras
  • 447
  • 2
  • 7
  • 18

4 Answers4

17

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.

Martino
  • 729
  • 6
  • 18
  • 4
    I believe this should be the correct answer. The question specifically asks for a way to "force install of package in a virtualenv despite it's presence in the system's global package folder". Hence, this answer provides the correct solution. Upgrading the packages like other answers suggested is the wrong option because this could change the specified version of a package in requirements.txt. – Teodoro Mar 14 '21 at 00:03
7

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

machnic
  • 2,304
  • 2
  • 17
  • 21
  • solved by removing one package from my requirements.txt, but still getting wrong version... when i run pip freeze, it shows 1.8.11 but when I run runserver it uses an the one. When I write 'which python', it shows my path (venv/bin/python)... is there any priority which package to use?? – Taras Oct 31 '18 at 10:58
  • It will use packages from the *active* environment. Are you sure that you activated the environment where you installed new version of `setuptools` and `Django` before running `python manage.py runserver`? (The name of active environment is displayed in brackets at the beginning of each line in terminal. You should see something like *(venv) myName@myHost:/some/path$* before you try to run the server.) – machnic Oct 31 '18 at 11:20
  • of course there is (venv), also tried venv/bin/python backend/manage.py runserver gives the same – Taras Oct 31 '18 at 11:32
  • Maybe, but as long as you are running all the commands like `python -m ...` with activated environment, sys paths shouldn't really matter. It's possible though that you made some mess with site-packages inside the environment. Have you tried to remove the environment completely, set up a new one and reinstall all packages using requirements.txt? – machnic Oct 31 '18 at 11:43
  • just tried to create a new venv, `venv/bin/python -m pip install -r config/requirements.txt` shows that installed but `venv/bin/python -m pip freeze` shows an old one – Taras Oct 31 '18 at 11:51
  • also after I create venv, i do `venv/bin/easy_install pip`, maybe the problem in it? because without it I am getting `ImportError: No module named _internal` – Taras Oct 31 '18 at 12:52
  • 1
    I added "step by step" part to my answer. Try to follow it exactly as is, without adding extra steps in the meantime. If it will not help then provide the full set of commands you used and their output. – machnic Oct 31 '18 at 13:42
  • so, followed all steps, but what I have django is taken from user dir, not from venv. when I do `python` or `venv/bin/python` and `>>> import django` `>>> django` `` only `venv/bin/pip freeze` shows me that I have updated Django version... `pip freeze` show the old, as well as `python -m pip freeze` and `./venv/bin/python -m pip freeze` (venv activated) – Taras Oct 31 '18 at 15:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182885/discussion-between-machnic-and-petriichuk). – machnic Oct 31 '18 at 16:26
0

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.

Taras
  • 447
  • 2
  • 7
  • 18
0

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

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • ... which is exactly contrary to what the OP wants: To force installing the package *within* the virtualenv and not on the system path. – taffit Jul 06 '22 at 09:05