Search code examples
pythonpipsix

Package version difference between pip and OS?


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?


Solution

  • 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.