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)
4
  • 1
    add --upgrade at the end of pip command
    – ruddra
    Oct 31, 2018 at 7:12
  • nope, doesn't work. Showing that installed but 'pip list' shows that not
    – Taras
    Oct 31, 2018 at 7:18
  • uninstall the package and re install it with pip, and make sure you remove all folders related in lib/site-packages....
    – Lemayzeur
    Oct 31, 2018 at 7:23
  • 1
    some-why it uses setuptools from system, not from venv
    – Taras
    Oct 31, 2018 at 8:04

4 Answers 4

18

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.

1
  • 5
    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, 2021 at 0: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

9
  • 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, 2018 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, 2018 at 11:20
  • of course there is (venv), also tried venv/bin/python backend/manage.py runserver gives the same
    – Taras
    Oct 31, 2018 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, 2018 at 11:43
  • 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, 2018 at 13:42
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.

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

1
  • ... 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 6, 2022 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.