Python virtual environments: Difference between revisions

From Stat DS Computing Wiki
Created page with " Virtual environments are useful tools to manage packages and the associated dependencies. In modern statistical practice, they are also associated with producing reproducible..."
 
replace conda with mamba
Line 2: Line 2:
Virtual environments are useful tools to manage packages and the associated dependencies. In modern statistical practice, they are also associated with producing reproducible results. Virtual environments also help isolate potentially problematic packages from your “base” environment that can be useful when using certain packages.
Virtual environments are useful tools to manage packages and the associated dependencies. In modern statistical practice, they are also associated with producing reproducible results. Virtual environments also help isolate potentially problematic packages from your “base” environment that can be useful when using certain packages.


Like everything in life nowadays, you have a few options that can help you make virtual environments. We will mention and go through the basics of both, but strongly recommend using <code>conda</code> and not <code>virtualenv</code>.
Like everything in life nowadays, you have a few options that can help you make virtual environments. We will mention and go through the basics of each, but strongly recommend using <code>mamba</code> and not <code>virtualenv</code>.


The two options are
The two options are


#<code>conda</code>  
#<code>mamba</code>
#<code>virtualenv</code>  
#<code>virtualenv</code>


Note that&nbsp;<code>conda</code>&nbsp;is actually a language-agnostic package manager. For example, it can be used to manage both Pyhon and R within the same virtual environment.&nbsp;Interested in R-specific virtual environments? Check&nbsp;[[Renv|virtual environments for R]].
Note that&nbsp;<code>mamba</code> is actually a language-agnostic package manager. For example, it can be used to manage both Python and R within the same virtual environment. Interested in R-specific virtual environments? Check [[Renv|virtual environments for R]].


= A note on installing packages =
(<code>conda</code> is no longer recommended as a virtual environment manager due to licensing issues. Instructions for setting up <code>conda</code> is still available [[Conda with Python (Reference Only)|here]] for reference)


There are two main ways to install python packages, either through <code>pip</code> or <code>conda</code>.
=A note on installing packages=
 
There are two main ways to install python packages, either through <code>pip</code> or <code>mamba</code>.
<syntaxhighlight lang="bash">pip install scikit-learn</syntaxhighlight>
<syntaxhighlight lang="bash">pip install scikit-learn</syntaxhighlight>


or
or
<syntaxhighlight lang="bash">conda install scikit-learn</syntaxhighlight>
<syntaxhighlight lang="bash">mamba install scikit-learn</syntaxhighlight>


<code>pip</code> will provide you with the most up-to-date package and <code>conda</code> only caries a subset of all of the packages avaiable on <code>pip</code> (aka from <code>pypi</code>). A major difference between <code>pip</code> installation of packages and <code>conda</code> installation is that <code>pip</code> doesn’t check the dependence struture of packages (this is required to be done manually), whereas <code>conda</code> makes sure no dependences are broken. This can become very useful especially if you don’t love constantly debugging. If a package cannot be obtained with <code>conda</code>, <code>conda</code> will still track packages you install using <code>pip install</code>.
<code>pip</code> will provide you with the most up-to-date package and <code>mamba</code> only caries a subset of all of the packages avaiable on <code>pip</code> (aka from <code>pypi</code>). A major difference between <code>pip</code> installation of packages and <code>mamba</code> installation is that <code>pip</code> doesn’t enforce dependencies (as of pip 20.3 released in November 2020, it will give a warning when dependencies are broken) whereas <code>mamba</code> makes sure no dependences are broken. This can become very useful especially if you don’t love constantly debugging. If a package cannot be obtained with <code>mamba</code>, <code>mamba</code> will still track packages you install using <code>pip install</code>.


N.B. Altough this is a nice&nbsp;<code>conda</code> functionality, it can behave annoingly sometimes. For example, conda retrieves packages from different “repositories” called channels (<code>-c</code> option when running <code>conda install</code>). Sometimes using different channels can yield different results and download slightly different versions of the same package, which easily causes inconsistencies in the environment. We strongly suggest to use the same channel within an environment, and only change it if&nbsp;a package is only available from a specific channel.
Although this is a nice <code>mamba</code> functionality, it can behave annoying sometimes. For example, <code>mamba</code> retrieves packages from different “repositories” called channels (<code>-c</code> option when running <code>mamba install</code>). Sometimes using different channels can yield different results and download slightly different versions of the same package, which easily causes inconsistencies in the environment. We strongly suggest to use the same channel within an environment, and only change it if a package is only available from a specific channel.


If a python package is just on a GitHub repository&nbsp;and&nbsp;not on <code>pypi</code> or <code>conda</code>, then you can download/clone the repository, navigate to the repository and then run
If a python package is just on a GitHub repository and not on <code>pypi</code> or <code>mamba</code>, then you can download/clone the repository, navigate to the repository and then run
<syntaxhighlight lang="bash">pip install .</syntaxhighlight>
<syntaxhighlight lang="bash">pip install .</syntaxhighlight>


By adding the option&nbsp;<code>-e</code> to the command above, the cloned repository is installed in editable mode, which allows you to modify that&nbsp;code locally and immediately use the changes by importing the repository in your code as an installed package. This is particularly useful when you are actively developing a package locally.
By adding the option&nbsp;<code>-e</code> to the command above, the cloned repository is installed in editable mode, which allows you to modify that code locally and immediately use the changes by importing the repository in your code as an installed package. This is particularly useful when you are actively developing a package locally.


= <code>conda</code> and virtual environments =
==Installing Mamba ==


As mentioned above, <code>conda</code> should really be thought of as a general package manager, but can also be used to create virtual environments.
===<code>mamba</code> for your personal computer===


== Installing Conda ==
To install <code>mamba</code> on your personal computer you can install "Mamba" or "Micromamba" with an installer (works for Mac, Windows and Linux) or via command line. For must use cases, "Mamba" will be sufficient, but see here for a [https://mamba.readthedocs.io/en/latest/user_guide/micromamba.html#micromamba description of Micromamba] and how it differs from "Mamba". To install Mamba head [https://mamba.readthedocs.io/en/latest/installation/mamba-installation.html here], and for Micromamba head [https://mamba.readthedocs.io/en/latest/installation/micromamba-installation.html here].


=== <code>conda</code> for your personal computer ===
===<code>mamba</code> for your server ===


To install&nbsp;<code>conda</code> on your personal computer you can install anaconda or miniconda with an installer (works for Mac, Windows and Linux) or via command line. Anaconda takes up more space and comes with a decent amount of packages installed, whereas miniconda takes up much less space but doesn't come with packages installed (see [https://conda.io/projects/conda/en/latest/user-guide/install/download.html#anaconda-or-miniconda conda.io] for more commentary on the differences. For either installation, check the hash (a unique identifier of the object) to make sure you're not installing a bug (see [https://conda.io/projects/conda/en/latest/user-guide/install/download.html#id5 | conda.io] for how to check the hash on different OS). Generally speaking, Anaconda can be useful if you're&nbsp;new to programming. Miniconda has all the package-manager functionalities but requires you to install every single package in the environments, which is generally better if you want to manage your projects from scratch.
To install <code>mamba</code> on the server you need to use the command line all the way through (you cannot download anything from a webpage. As such, I recommend installing mamba following these steps (for Linux systems):


To install miniconda head here: [https://docs.conda.io/en/latest/miniconda.html docs.conda] and anaconda head here: [https://docs.anaconda.com/anaconda/install/index.html docs.anaconda].
#<pre>mkdir -p ~/mamba</pre>
 
=== <code>conda</code> for your server ===
 
To install <code>conda</code> on the server you need to use the command line all the way through (you cannot download anything from a webpage. As such, I recommend installing miniconda following these steps (for Linux systems):
 
#<pre>mkdir -p ~/miniconda3</pre>


<ol start="2">
<ol start="2">
<li><pre>wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
<li><pre>wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O ~/mamba/install.sh
</pre>
</pre>
</li>
</li>
<li><pre>bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
<li><pre>bash ~/mamba/install.sh -b -u -p ~/mamba
</pre>
</pre>
</li>
</li>
<li><pre>rm -rf ~/miniconda3/miniconda.sh</pre>
<li><pre>rm -rf ~/mamba/install.sh</pre>
</li>
</li>
<li><pre>~/miniconda3/bin/conda init bash</pre>
<li><pre>~/mamba/bin/conda init bash</pre>
</li>
</li>
</ol>
</ol>


Similar steps can be followed to install miniconda from the terminal on Mac and Windows systems. See&nbsp;this [https://engineeringfordatascience.com/posts/install_miniconda_from_the_command_line/ link].<br/> '''N.B. for Hydras.'''&nbsp;For reasons related to how Hydras directory system is setup, you will need an extra step to make sure miniconda is initialized and available every time you log into the server. The steps above populate your&nbsp;<code>.bashrc</code> file with a small script that initialized conda at log in, but this file is not sourced by Hydras. Instead, you need to create a&nbsp;<code>.profile</code> file (if it's not already there) and add&nbsp;the following line to it:
Similar steps can be followed to install minimamba from the terminal on Mac and Windows systems. <br />'''Note for Hydra Users:'''&nbsp;For reasons related to how Hydras directory system is setup, you will need an extra step to make sure mamba is initialized and available every time you log into the server. The steps above populate your&nbsp;<code>.bashrc</code> file with a small script that initialized mamba at log in, but this file is not sourced by Hydras. Instead, you need to create a&nbsp;<code>.profile</code> file (if it's not already there) and add&nbsp;the following line to it:
<pre>source .bashrc</pre>
<pre>source .bashrc</pre>




== Conda’s virtual environments ==
==Mamba's virtual environments==


A good tutorial on how to create a virtual environment with <code>conda</code> can be found at [https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands conda-io]
A good tutorial on how to create a virtual environment with <code>mamba</code> can be found at [https://mamba.readthedocs.io/en/latest/user_guide/mamba.html here]


In a nutshell:
In a nutshell:


#To a&nbsp;'''create new environment'''&nbsp;with Python installed (note that <code>base</code> is your standard environment) you should run: <syntaxhighlight lang="bash">conda create -n yourenvname python=x.x</syntaxhighlight>
#To a&nbsp;'''create new environment'''&nbsp;with Python installed (note that <code>base</code> is your standard environment) you should run: <syntaxhighlight lang="bash">mamba create -n yourenvname python=x.x</syntaxhighlight>




&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where <code>x.x</code> is the python version you’d like to use. I might suggest starting with 3.11&nbsp;now.
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;where <code>x.x</code> is the python version you’d like to use. I might suggest starting with 3.11&nbsp;now.
<ol start="2">
<ol start="2">
<li>Then to '''activate your conda environment''', just run: <syntaxhighlight lang="bash">conda activate yourenvname</syntaxhighlight>
<li>Then to '''activate your conda environment''', just run: <syntaxhighlight lang="bash">mamba activate yourenvname</syntaxhighlight>
</li>
</li>
</ol>
</ol>
Line 80: Line 76:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;This will probably make your terminal show <code>(yourenvname) ...$</code>, whereas&nbsp;originally it should have read <code>(base) ...$</code>.
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;This will probably make your terminal show <code>(yourenvname) ...$</code>, whereas&nbsp;originally it should have read <code>(base) ...$</code>.
<ol start="3">
<ol start="3">
<li>To '''install packages''' in your local environment:<br/> After activating (and before deactivating your virtual environment) you can just do <syntaxhighlight lang="bash">conda activate yourenvname
<li>To '''install packages''' in your local environment:<br /> After activating (and before deactivating your virtual environment) you can just do <syntaxhighlight lang="bash">mamba activate yourenvname
conda install [package]</syntaxhighlight>
mamba install [package]</syntaxhighlight>
</li>
</li>
</ol>
</ol>
Line 87: Line 83:
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;An alternative to the second line is&nbsp;&nbsp;<code>pip install [package]</code>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;An alternative to the second line is&nbsp;&nbsp;<code>pip install [package]</code>
<ol start="4">
<ol start="4">
<li>To '''deactivate''' your environment: <syntaxhighlight lang="bash">conda deactivate</syntaxhighlight>
<li>To '''deactivate''' your environment: <syntaxhighlight lang="bash">mamba deactivate</syntaxhighlight>
</li>
</li>
</ol>
</ol>
<ol start="5">
<ol start="5">
<li>To '''share''' virtual environments (useful for reproducibility):<br/> To share your virtual environment (with collaborators or yourself on the server) you can make a <code>.yml</code> file that contains all the information about the packages you are using by first opening up the virtual environment and then running <syntaxhighlight lang="bash"> conda env export > environment.yml</syntaxhighlight>
<li>To '''share''' virtual environments (useful for reproducibility):<br /> To share your virtual environment (with collaborators or yourself on the server) you can make a <code>.yml</code> file that contains all the information about the packages you are using by first opening up the virtual environment and then running <syntaxhighlight lang="bash"> mamba env export > environment.yml</syntaxhighlight>
</li>
</li>
</ol>
</ol>


&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Then you can share the <code>environment.yml</code> file to another computer, and with that computer create a copy of the environment via&nbsp; &nbsp;<code data-lang="bash">conda env create -f environment.yml</code>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Then you can share the <code>environment.yml</code> file to another computer, and with that computer create a copy of the environment via&nbsp; &nbsp;<code data-lang="bash">mamba env create -f environment.yml</code>
 
More guidance can be found here: [https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#sharing-an-environment conda.io]


= <code>virtualenv</code> virtual environments =
=<code>virtualenv</code> virtual environments=


If you’re only using <code>pip</code> as your package installer you can also get virtual environments using <code>virtualenv</code>. These environments require you to specify a specific location for their information to be stored (so often are project dependent). Some good references are [https://docs.python-guide.org/dev/virtualenvs/ docs.python-guide] and [https://realpython.com/python-virtual-environments-a-primer/ realpython].
If you’re only using <code>pip</code> as your package installer you can also get virtual environments using <code>virtualenv</code>. These environments require you to specify a specific location for their information to be stored (so often are project dependent). Some good references are [https://docs.python-guide.org/dev/virtualenvs/ docs.python-guide] and [https://realpython.com/python-virtual-environments-a-primer/ realpython].
Line 108: Line 102:
Then to create a new virtual environment and use it, you can follow these basic steps:
Then to create a new virtual environment and use it, you can follow these basic steps:


#You should select where you’d like to store the information about the virtual environment.  
#You should select where you’d like to store the information about the virtual environment.
#To '''create''' a new virtual environment, in that folder you can run (right in the terminal): <syntaxhighlight lang="bash">virtualenv yourenvname</syntaxhighlight>
#To '''create''' a new virtual environment, in that folder you can run (right in the terminal): <syntaxhighlight lang="bash">virtualenv yourenvname</syntaxhighlight>



Revision as of 03:25, 21 February 2025

Virtual environments are useful tools to manage packages and the associated dependencies. In modern statistical practice, they are also associated with producing reproducible results. Virtual environments also help isolate potentially problematic packages from your “base” environment that can be useful when using certain packages.

Like everything in life nowadays, you have a few options that can help you make virtual environments. We will mention and go through the basics of each, but strongly recommend using mamba and not virtualenv.

The two options are

  1. mamba
  2. virtualenv

Note that mamba is actually a language-agnostic package manager. For example, it can be used to manage both Python and R within the same virtual environment. Interested in R-specific virtual environments? Check virtual environments for R.

(conda is no longer recommended as a virtual environment manager due to licensing issues. Instructions for setting up conda is still available here for reference)

A note on installing packages

There are two main ways to install python packages, either through pip or mamba.

pip install scikit-learn

or

mamba install scikit-learn

pip will provide you with the most up-to-date package and mamba only caries a subset of all of the packages avaiable on pip (aka from pypi). A major difference between pip installation of packages and mamba installation is that pip doesn’t enforce dependencies (as of pip 20.3 released in November 2020, it will give a warning when dependencies are broken) whereas mamba makes sure no dependences are broken. This can become very useful especially if you don’t love constantly debugging. If a package cannot be obtained with mamba, mamba will still track packages you install using pip install.

Although this is a nice mamba functionality, it can behave annoying sometimes. For example, mamba retrieves packages from different “repositories” called channels (-c option when running mamba install). Sometimes using different channels can yield different results and download slightly different versions of the same package, which easily causes inconsistencies in the environment. We strongly suggest to use the same channel within an environment, and only change it if a package is only available from a specific channel.

If a python package is just on a GitHub repository and not on pypi or mamba, then you can download/clone the repository, navigate to the repository and then run

pip install .

By adding the option -e to the command above, the cloned repository is installed in editable mode, which allows you to modify that code locally and immediately use the changes by importing the repository in your code as an installed package. This is particularly useful when you are actively developing a package locally.

Installing Mamba

mamba for your personal computer

To install mamba on your personal computer you can install "Mamba" or "Micromamba" with an installer (works for Mac, Windows and Linux) or via command line. For must use cases, "Mamba" will be sufficient, but see here for a description of Micromamba and how it differs from "Mamba". To install Mamba head here, and for Micromamba head here.

mamba for your server

To install mamba on the server you need to use the command line all the way through (you cannot download anything from a webpage. As such, I recommend installing mamba following these steps (for Linux systems):

  1. mkdir -p ~/mamba
  1. wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh -O ~/mamba/install.sh
    
  2. bash ~/mamba/install.sh -b -u -p ~/mamba
    
  3. rm -rf ~/mamba/install.sh
  4. ~/mamba/bin/conda init bash

Similar steps can be followed to install minimamba from the terminal on Mac and Windows systems.
Note for Hydra Users: For reasons related to how Hydras directory system is setup, you will need an extra step to make sure mamba is initialized and available every time you log into the server. The steps above populate your .bashrc file with a small script that initialized mamba at log in, but this file is not sourced by Hydras. Instead, you need to create a .profile file (if it's not already there) and add the following line to it:

source .bashrc


Mamba's virtual environments

A good tutorial on how to create a virtual environment with mamba can be found at here

In a nutshell:

  1. To a create new environment with Python installed (note that base is your standard environment) you should run:
    mamba create -n yourenvname python=x.x


           where x.x is the python version you’d like to use. I might suggest starting with 3.11 now.

  1. Then to activate your conda environment, just run:
    mamba activate yourenvname

           This will probably make your terminal show (yourenvname) ...$, whereas originally it should have read (base) ...$.

  1. To install packages in your local environment:
    After activating (and before deactivating your virtual environment) you can just do
    mamba activate yourenvname
    mamba install [package]

           An alternative to the second line is  pip install [package]

  1. To deactivate your environment:
    mamba deactivate
  1. To share virtual environments (useful for reproducibility):
    To share your virtual environment (with collaborators or yourself on the server) you can make a .yml file that contains all the information about the packages you are using by first opening up the virtual environment and then running
     mamba env export > environment.yml

           Then you can share the environment.yml file to another computer, and with that computer create a copy of the environment via   mamba env create -f environment.yml

virtualenv virtual environments

If you’re only using pip as your package installer you can also get virtual environments using virtualenv. These environments require you to specify a specific location for their information to be stored (so often are project dependent). Some good references are docs.python-guide and realpython.

To start you should download virtualenv with

pip install virtualenv

Then to create a new virtual environment and use it, you can follow these basic steps:

  1. You should select where you’d like to store the information about the virtual environment.
  2. To create a new virtual environment, in that folder you can run (right in the terminal):
    virtualenv yourenvname
  1. To activate run:
    $ source yourenvname/bin/activate

           This will probably make your terminal show  (yourenvname) ...$

  1. To deactivate:
    (env) ... $ deactivate
    ... $