Skip to content

Python Environments

Connor Czarnuch edited this page Jan 16, 2021 · 3 revisions

Python environments are a very easy way to manage different Python projects on your local system. The reason that you may want to do this is if you are running different projects or if you are setting up a simple way to deploy Python projects.

Virtual Environments

A virtual environment is essentially a separate installation of python that can be contained in a single folder of your choosing. To get a virtual environment set up there are a couple steps.

  1. Install venv.
    There are several ways that you can accomplish this step. The easiest way is to run the command

     python -m pip install --user virtualenv
    

    Change the Python version according to which version of python you are using or how your system is set up.

  2. Change directory to the destination directory.
    If you are familiar with the command line then this should be relatively simple. Just use the cd command to navigate to your projects directory. Best Practice: although it is possible for a virtual environment to be created anywhere, a best practice is to create the virtual environment in the root folder of the project it is going to be used with. That way it is easy to keep track of which vent belongs to which project.

     cd foo/bar/
    
  3. Create the Virtual Environment.
    Creating the virtual environment can also be done with a simple command.

     python -m venv [virtual environment name]
    

    This will create a virtual environment with the designated name at the current directory. Best Practice: the most common way to name your virtual environments is venv or env. It does not matter which you choose, but these names are the most common and will likely be picked up by your IDE which can automatically use the virtualenv as the Python interpreter when you are developing code.


PyEnv

Once you have a grasp on virtual environments, then the next logical step is to take a look at PyEnv. PyEnv is the easiest way to manage different versions of Python on a single system.

  1. Installing PyEnv.
    PyEnv was originally designed for MacOS by using homebrew (link). However, it is possible to download and use on windows as well.

    a. MacOS
    Using the link above, install Homebrew. After homebrew is installed, installing pyenv is as easy as running

     brew install pyenv
    

    You should now be able to run the pyenv --version command. If this works, then continue to set environment variables using this command.

     eval "$(pyenv init -)"
    

    After this, you can either restart your terminal or run the command

     exec "$SHELL"
    

    to complete the installation.

    b. Windows
    Using the python package manager pip you can install PyEnv on windows platforms as well. If Python is installed on your system, then you are able to enter the following command to install PyEnv on your system

     pip install pyenv-win --target %USERPROFILE%\.pyenv
    

    This should install PyEnv on your windows device. For more information see here link. To complete the installation of PyEnv you need to set environment variables in your system. To do this, open CMD and enter the following command.

     [System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
    

    Once that is done, add the following to your PATH variable.

     [System.Environment]::SetEnvironmentVariable('path', $HOME + "\.pyenv\pyenv-win\bin;" + $HOME + "\.pyenv\pyenv-win\shims;" + $env:Path,"User")
    

    After this, you need to close and reopen your terminal. Then finally, run the command

     pyenv rehash
    

    In case this command does not work, you may need to set the environment variables through the Windows GUI. Navigate to This PC → Properties → Advanced system settings → Advanced → Environment Variables... → PATH. Now the installation should be complete. Check if it is working by running pyenv --version.

  2. Installing a Python version.
    After the installation of PyEnv, you are ready to begin downloading Python distributions.

    The command

     pyenv install [version number]
    

    is mainly what you'll use to install new versions of Python.

    For the purposes of this wiki, we'll use version 3.6.1. The command we will run to install this version of Python is

     pyenv install 3.6.1
    

    Once this completes, check the versions that are installed by PyEnv by using the command

     pyenv versions
    

    There may be multiple versions listed already. One version will be the system. This is the version that is the system default and may be preinstalled by the system. Any other versions are the versions that are installed by PyEnv. To choose the version that we want to use is as easy as running the command

     pyenv global 3.6.1
    

    for version 3.6.1. You can change the version number as needed. What this command does is it sets the global Python version to 3.6.1. If you wish to set the python version for a specific directory, you can replace the global keyword with local. You can check if it was successful by using the command python --version and checking if it is correct. If it isn't what you expect, then run the following command and check if the output matches.

     % which python
     /Users/[username]/.pyenv/shims/python
    

    If this is not the output you get, then check if your PyEnv installation was successful by following the steps above.\

  3. Troubleshooting.
    Sometimes, PyEnv will be unable to install a specific python version. In cases like these, usually running the command below will be able install the Python version required.

     CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" \
     LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" \
     pyenv install --patch [VERSION NUMBER] < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
    

    Replace the [VERSION NUMBER] text with the version number required.

Using them together

The best way to manage Python versions on your system and for different projects is by using these two programs in conjunction. First, download the Python version that you wish to use for that project, then in the project directory create a new virtual environment after setting the Python version using PyEnv. This will make your time coding using different Python versions much easier and may save a lot of headaches in the future.

Clone this wiki locally