6

I have installed and configured python from online tutorials, python books, and other various sources in the past only to find myself digging through forums when things go wrong and piecing various information together to try and reach my objective. Which is a clean installation/configuration of python 3.X.X that does not interfere with the system's default interpreter, has pip, easy install, distribute tools, and virtualenv all properly configured and working; can be used side-by-side with the system's python interpreter, is installed in the /opt/ directory (the proper location), is named clearly in the /opt/ directory (e.g. python3.3.0, python3.3.1, python3.3.2), and can be called from the terminal window without using alias (i.e. the path is setup and working). This is a lot of criteria, but is the setup I'm looking for. When following online tutorials, I can typically achieve a few of these, but never all of them.

The closest tutorial I have found so far is here (How do I install Python 3.3?), but it still doesn't provide enough clarification or has broken steps. For example, the answer with the most votes (by Sergey and minerz029) has the following issues:

  • Does not explain how to setup pip, easy install, distribute tools, and virtualenv. It appears impossible with those steps because they do not include setting up the proper dependencies before compiling to get distribute tools (necessary to get pip) prior to compiling (as indicated here (https://stackoverflow.com/questions/18833442/installing-distribute-for-python-3-3). But then when trying to follow the second link, openssl-devel doesnt seem available from apt-get.

  • Does not setup the python path but rather uses a systemlink.

All the tutorials I follow start a chain reaction in going from forum to forum with no clear answer because everyone uses different steps, commands, methodologies etc. and often leave out key points that start the hunt all over again. Can anyone take a look at the first link and explain how to get pip, easy_install working? I have also followed the second author's instructions (i.e. “Boolean”) but his steps break at step 3 and appears to exclude key points between steps 2 and 3 in switching directories.

Kryptos
  • 203
  • 1
  • 2
  • 7
  • 1
    Do you want your /opt installation to be a virtualenv? – kiri Jan 17 '14 at 07:34
  • 1
    I believe so. My understanding is that it will eliminate the breakage of each python3.X.X's installation dependencies. But I'm still learning python. – Kryptos Jan 17 '14 at 07:42

1 Answers1

5

These steps will create a Python 3.3 virtualenv in a /opt subdirectory:

  1. Install Pip and virtualenv on your system Python installation.

    sudo apt-get install python-pip
    sudo -H pip install virtualenv
    
  2. Run this command (see notes below):

    sudo -H virtualenv /opt/python_custom -p python3
    
    • Replace /opt/python_custom with the path you would like to install to.
    • Replace python3 with the interpreter which will be used, e.g.: python2, python3.3 or others.
  3. If you want to operate on this Python installation, you will need to run this command first:

    source /opt/<PYTHON>/bin/activate
    

    (replace <PYTHON> with the path from above)

    Your shell prompt will change to reflect the current virtualenv.

  4. You can then use sudo pip to install whichever packages you would like, e.g.:

    sudo pip install setuptools
    sudo pip install distribute
    

Note: To use this Python in your scripts, specify the #! line to be the virtualenv interpreter, e.g.:

#!/opt/python_custom/bin/python

Note: You'll need to use sudo when changing the virtualenv (e.g.: by installing a package with pip) because it is installed into /opt, which is owned by root.

kiri
  • 28,246
  • 16
  • 81
  • 118
  • I was not able to get past the first command. Here is the result of my "uname -a" Linux kryptos-Darkfleet 3.5.0-18-generic #29-Ubuntu SMP Thu Oct 25 07:26:14 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux. I'm running linux mint 13 LTS – Kryptos Jan 17 '14 at 07:55
  • 1
    kryptos@kryptos-Darkfleet ~ $ sudo apt-get install python3-pip Reading package lists... Done Building dependency tree
    Reading state information... Done E: Unable to locate package python3-pip kryptos@kryptos-Darkfleet ~ $
    – Kryptos Jan 17 '14 at 07:58
  • I checked my sources list and also here: http://packages.ubuntu.com/search?suite=precise§ion=all&arch=amd64&keywords=pip&searchon=names . I'm still investigating... It doesnt look like pip3 is an available package here: http://packages.ubuntu.com/search?suite=precise§ion=all&arch=amd64&keywords=pip&searchon=names – Kryptos Jan 17 '14 at 08:26
  • 1
    The package is missing for precise and no longer available. It is available for other versions. Not sure how to report or fix it. http://packages.ubuntu.com/search?suite=all&arch=amd64&searchon=names&keywords=pip – Kryptos Jan 17 '14 at 08:38
  • 1
    In your step, 2, can "python3" be replaced with "python" (the system default python2.X) and if so, will this setup still work with virtual python3 if I never ran apt-get install python3? I'm trying to clean up the mess I made that night you helped me by following your instructions on a brand new install and ran into issues. http://askubuntu.com/questions/408192/how-to-install-python-3-x-x – Kryptos Jan 20 '14 at 13:50
  • After this is activated, I get (python3.3.0)kryptosvm@kryptosvm-VirtualBox ~ $. So if I sudo pip install bottle, how do I "import bottle" in the virtual python 3? I get, ImportError: No module named 'bottle' after I open python's shell with: "/opt/python3.3.0/bin/python3" – Kryptos Jan 20 '14 at 14:20
  • In your step 4, you should not use sudo. Once you have your virtualenv activated, there is no need for sudo since you are installing to a location you own (the virtualenv). Also, using sudo pip install while a virtualenv is active will cause permissions problems. – Christian Long Sep 28 '16 at 20:34
  • Also, this answer does not address the question of how and where to install different python versions (Python 3.3.2, 3.4.1, etc.). The pyenv (not pyvenv) tool can help you manage different user-installed Python versions on the same machine. – Christian Long Sep 28 '16 at 20:37