Python Environments
Published:
Environments in Anaconda and Python allow you to create a named and isolated copy of Python. Within these environments you can work with specefic versions of Python and packages without affecting your base installation. These enviroments make it easy to:
- seperate projects which can rely on both specfic versions of Python and packages (like numpy)
- test, install, and develop new packages without breaking your base installation
- collaborate with others providing a simple way to share/mirror environments.
Anaconda very simply creates and manages these enviroments using conda though similar Virtual Environemnts can be created in Python with virtualenv.
This is a sample blog post. Lorem ipsum I can’t remember the rest of lorem ipsum and don’t have an internet connection right now. Testing testing testing this blog post. Blog posts are cool.
Environments - An example
I recently had trouble installing a Python version of the International Geomagnetic Reference Field (IGRF) in order to calculate magnetic declinations. I was attempting to install Michael Hirsch’s scivision IGRF12 package from GitHub. Upon installation I kept running into an error with the numpy
package. The IGRF12 installation required a version of numpy that wasn’t yet available through Anaconda and which conflicted with other package installtions I had. A new Python environment solved both these problems.
Within the IGRF12 directory retrieved from GitHub the following solved all my problems.
conda info --env #list all environments
conda create --name igrf12 python=3.6 cmake pytest #create a new environment
conda activate igrf12 #activate the new environment
python -m pip install -e . #install igrf12
pytest #test installation
The numpy
error could have also been solved by updating numpy through another channel like conda-forge but a seperate Environment allowed IGRF12 to be installed without affecting anything else.
The above allowed me to install IGRF in a bare bones environment, additional packages could then be easily installed with conda install
and pip
.
Evironments - Some Basics
There are several resources online that go into detail regarding Environments and one of the best is the Anaconda docs. Regardless here are some of the basics regarding Environments.
In the Anaconda prompt the base installation is identified by (base)
at the start of the command line, which also identifies the currently active environment. To create and activate a new enviroment with the same Python version as you base installation you use:
# create and activate a new environment
conda create --name test
conda activate test
# within the new environment you can install
# packages with pip and conda
#deactivate the new environment
conda deactivate
# create an environement with a specfic
# version of Python
conda create --name Py27 python=2.7
# or with specefic packages
conda create --name test2 scipy numpy matplotlib
# list the avialable environments
conda env list
conda info --env
# to remove an environment
conda remove --name test2 --all
The Anaconda docs on Environtments goes into more detail regarding the above commands. It also discusses more complex tasks like sharing an environment for collaborations, which we’ll cover here in a future post. There are also numerous web resources regarding Python environments that may be helpful including:
- Towards Data Science, Anaconda environments in PyCharm.
- freeCodeCamp, a more detailed discussion regarding environments.