Ruprecht-Karls-Universität Heidelberg

Managing your own Python installation

Introduction

Due to the large number of variants of Python versions, modules and module versions, we unfortunately can't offer a system-wide installation of Python suiting everyone's need on our Linux workstations and compute servers.  Instead, you can quite easily install your own environment with everything exactly as you need it.

This can be accomplished with the help of pyenv and virtualenv (as a plugin), which allows you manage a completely customized Python environment.

In the following, we assume that you haven't actually done any of those steps outlined below, so you don't have pyenv or virtualenv installed and there is no directory named ~/.pyenv in your home directory and also neither pyenv or virtualenv are referenced in your ~/.bashrc or ~/.bash_profile (or similar files for other shells) and we will use a set of example modules.

 

 

  1. Install pyenv:

    git clone https://github.com/pyenv/pyenv.git ~/.pyenv 
    # import environment into bash configuration:
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
    echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
  2. Restart shell or logout/login

  3. Install a new Python version. 3.6.1 is the newest at the time of writing and appears to be working (this takes some time):

    pyenv install 3.6.1 
  4. Note: With this method, you can install plain CPython versions as well as distributions like anaconda. To get a list of Python versions available, you can use pyenv install --list
  5. Install the virtualenv plugin for pyenv:

     git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv  
    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
  6. Again, restart shell or logout/login

  7. You can now create a new virtual python environment for your project.

     pyenv virtualenv 3.6.1 nbody  

    will create an environment named nbody that uses Python 3.6.1

  8. You can activate this environment with

    pyenv activate nbody  

but if you create a file named .python-version into any directory, you will automatically switch to the python version and environment listed in that file. So, let’s test this:

 

    mkdir nbody
    echo 'nbody' > .python-version 

Your prompt should now change and begin with nbody and python --version should result in 3.6.1. If you leave this directory, the prompt will change again, and python --version will show the system python version.

  1. Ok, now we are ready for pynbody. Either use pyenv activate or switch into the nbody directory to activate our environment. Then do the following:

     pip install matplotlib numpy scipy cython   # if you want to use the released version   pip install pynbody   # and for bleeding edge:   pip install git+git://github.com/pynbody/pynbody.git  # pyenv/virtualenv would make it really easy to switch between those two, just create a new environment for it.  

If everthing worked out, the following should work:

 

   python
       Python 3.6.1 (default, May 10 2017, 16:53:10)
       [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
       Type "help", "copyright", "credits" or "license" for more information.

       >>> import pynbody

That’s it.

(Further reading: https://github.com/pyenv/pyenv and https://github.com/pyenv/pyenv-virtualenv).

zum Seitenanfang/up