1

I have Debian OS and python version 2.7 installed on it. But I have a strange issue about package six. I want to use 1.10 version.

I have installed six 1.10 via pip:

$ pip list
...
six (1.10.0)

But when I run the following script

python -c "import six; print(six.__version__)"

it says 1.8.0

The reason is that veriosn installed in OS is different:

$ sudo apt-cache policy python-six
python-six:
Installed: 1.8.0-1
Candidate: 1.8.0-1
Version table:
    1.9.0-3~bpo8+1 0
      100 http://172.24.70.103:9999/jessie-backports/ jessie-backports/main amd64 Packages
*** 1.8.0-1 0
    500 ftp://172.24.70.103/mirror/jessie-debian/ jessie/main amd64 Packages
    500 http://172.24.70.103:9999/jessie-debian/ jessie/main amd64 Packages
    100 /var/lib/dpkg/status

How to force python to use package installed via pip?

martineau
  • 119,623
  • 25
  • 170
  • 301
Lecko
  • 1,275
  • 1
  • 17
  • 32
  • 1
    I am also running Debian Jessie and have `six-1.10.0` installed, but I don't see the behavior you're getting. What is the output of `python -c "from pkg_resources import Environment;print(Environment()['six'])"`? – wkl Sep 20 '16 at 11:29
  • 1
    where is this package installed with pip? Using the `--user` flag or in `/usr/local`? – languitar Sep 20 '16 at 11:36
  • @birryree [six 1.10.0 (/usr/local/lib/python2.7/dist-packages)] – Lecko Sep 20 '16 at 12:06
  • @languitar /usr/local/lib/python2.7/dist-packages – Lecko Sep 20 '16 at 12:06
  • 2
    Can you please add the output of: `python -c 'import sys; print("\n".join(sys.path))'` as well as `python -c 'import six; print(six)'` – languitar Sep 20 '16 at 12:53
  • 1
    @languitar thanks a lot for your help. After executing your last commands I have found that I have 2 python distributions...one in `/usr/lib` and another in `/usr/local/lib`. After replacing six in the first, everything worked – Lecko Sep 20 '16 at 13:33
  • Maybe answering this question yourself and accepting that answer would then better reflect the issue. – languitar Sep 20 '16 at 14:04

1 Answers1

1

You can use virtualenv for this.

pip install virtualenv

cd project_folder
virtualenv venv

virtualenv venv will create a folder in the current directory which will contain the Python executable files, and a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this case, it was venv) can be anything; omitting the name will place the files in the current directory instead.

Set the wished python interpreter

virtualenv -p /usr/bin/python2.7 venv

Activate the environment

source venv/bin/activate

From now on, any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.

pip install six

Now you run code. When you have finished simpliy deactivate venv

deactivate

See also the original resources.

nauer
  • 690
  • 5
  • 14
  • 1
    I almost never recommend `virtualenv`. `virtualenvwrapper` is so much cleaner and, in my opinion, has a much more simple entry point. – erip Sep 20 '16 at 11:33