0

I am new to Linux and have just installed ubuntu12.10 on my laptop. I use Python and there are two version's Python on ubuntu, 2.7 and 3.2. And here comes my question, if I download a module and then setup it, which version will it be installed to? I don't know the command such as sudo apt-get install python-virtualenv will install it to where, which version either. Is there a default version and then everything installed to it?

And when I want to use pip to install some packages, still I don't know which version it will match and I don't know how to install the package to the version I want. I have searched google and stackoverflow and know it has something to do with the virtualenv. And I read virtualenv document but I still don't understand it clearly.

Hope somebody can help me.

zhangyangyu
  • 8,520
  • 2
  • 33
  • 43
  • I have found an answer on http://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip and I find if on ubuntu just use `sudo apt-get install python3-pip` and then use pip-3.2 the package will be installed right. – zhangyangyu Apr 11 '13 at 05:31

3 Answers3

2

The Debian Python Policy describes how packaged modules are supposed to interact with dpkg so that add-ons are installed, compiled etc for each installed version if Python.

IIRC the policy states that python means python2 and you have to use python3 explicitly to get v3.

In practice, that means if you want to set up your own Python 3 environment with your own personal libraries, you will need a virtual environment (sort of like a vmware box or a chroot, except only for overriding the system-wide Python).

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

You can check which python you're running by issuing the following command

which python
[output-path]eg: /opt/local/bin/python

Use this

/opt/local/bin/python --version

If this is the python version you want to pointing at Use this path in the topmost line of your python script. and use

#!/opt/local/bin/python
Thunderboltz
  • 1,597
  • 3
  • 14
  • 16
0

There are two(or more) binary python interpreters in /usr/bin/. Each python executable script you run(manage.py etc) specifies in the beginning something like this

    #!/usr/bin/python2.6

That are system level interpreters. They store their libraries in

    /usr/local/lib/pythonXX/site-packages

With virtualenv you are free to have your own sand boxed python environments. Each environment will have unix-like directory structure including

    $PATH_TO_ENV/lib/site-packages #library home
    $PATH_TO_ENV/bin               #binaries home

when you activate virtualenv, and then run any python script it will use python executable from $PATH_TO_ENV/bin. All libraries are set up into $PATH_TO_ENV/lib/site-packages if you could remove $PATH_TO_ENV any time and do not worry about any garbage on system-level.

To set up virtualenvs I'm using something like this:

  sudo easy_install pip;
  sudo pip install virtualenv;
  sudo pip install virtualenvwrapper;

  echo "export WORKON_HOME=~/Envs" >> ~/.bashrc;
  echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc;
  . ~/.bashrc;
  mkdir -p $WORKON_HOME;

When using pip you could explicitly specify version number ex.

  pip install django-debug-toolbar>=0.9.4,<1.0 

if you are inside virtualenv package and all it's dependencies would be installed in

  $PATH_TO_ENV/lib/site-packages 
singer
  • 2,616
  • 25
  • 21