In the summer of 2018 I began a series of posts about setting up Python for anyone who is new to using it. My focus is predominantly on Geospatial Python, but I think the principals should be applicable to any application of Python. If you want a recap then part 1 (setting up in Anaconda) and part 2 (Jupyter Notebooks) are available using the links below:
In this post, part 3, I will cover virtual Python environments. I’ll explain what a virtual environment is and show you how to set one up using Anaconda.
Why use a Virtual Environment?
A virtual environment will allow you to try out new Python libraries without the risk of damaging or altering your main or base Python environment. This could be best practice if you have a working development environment and require several test beds, perhaps containing different versions of Python and/or different versions of various libraries. You could keep your base setup stable by testing new releases of libraries in a virtual environment. What do I mean by that? I mean you can check existing code to ensure it still works in the way you expect.
What is a Virtual Environment?
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.
The key word is isolated. Each virtual environment that you create will have its own dependencies, irrespective of any other Python environments you may (or may not) have. Oh, and you are not limited to the number of virtual environments you have. You are only limited by the hard disk space of your computer.
Setting up a Virtual Python Environment using Anaconda
A brief recap on this blog series… Anaconda is an Open Source distribution of Python (and R) with the overriding aim to make using Python as simple as possible. While there are other ways to install Python, I am using Anaconda and at this point I will assume that you are too. I will assume that you have the latest working version of Anaconda installed. If not, re-visit part 1 here.
The syntax to create a new virtual Python environment is shown below (in an Anaconda prompt):
conda create -n yourenvname python=x.x
This is nice because you can pick your version of Python. For example to create a new virtual Python environment called python_test37 using Python 3.7 use this command
conda create -n python_test37 python=3.7
You will get this prompt returned:
Type ‘y’ and press return to create your new environment. In this case it will install Python 3.7. The downloading of the packages will begin and then will install. You should get a message saying that the installation has been installed correctly and helpfully you will receive the prompt telling you how to activate this new virtual environment.
In this example the command is:
conda activate python_test37
You will know you are now working in this virtual Python environment as the name in brackets will change to the activated environment. I have highlighted this in red below:
Now we can type:
and the Python environment will be working. Test by importing one of the standard libraries, such as ‘os’.
If all is working then no errors should be returned.
This will exit the Python interpreter. To install other libraries follow the same method as described in part 1. For example, to install gdal, type:
conda install -c conda-forge gdal
Continue to install different packages as needed.
You can access Jupyter Notebook without having to install anything else. The same applies as in part 2, available here.
Once you have finished using this environment, you can leave by the command:
There is no need to specify the environment name. With this command you will return to (base), the default environment.
What has been installed and how do I get rid of it?
Finally, let’s take a quick look at what has been installed. On my machine (where my installation of Anaconda resides) I have a folder called env that holds all my virtual Python environments. My location is …\Anaconda3\envs. If you go and have a look you will find a lot of files (dll’s, and .py files mainly).
If you wish to remove a virtual Python environment, at the command line type:
conda env remove -n python_test37
as shown in the screen shot below:
By selecting ‘y’ and hitting return you will remove the virtual Python vnvironment in Anaconda.
In this third part we have built on our installed Anaconda Python environment from part 1 and created a virtual Python environment. Build a virtual environment every time you wish to test a new library/package, or if you wish to use different Python versions (and library versions), and/or if you wish to maintain a stable development environment without upgrading packages when you are not 100% sure of the impact on your existing Python setup/code.
This series is really meant to be for complete beginners who have no familiarity with these environments. If you have any comments, questions or suggestions, drop me an email on firstname.lastname@example.org
I am a freelancer able to help you with your projects. I offer consultancy, training and writing. I’d be delighted to hear from you. Please check out the books I have written on QGIS 3.4
I have grouped all my previous blogs (technical stuff / tutorials / opinions / ideas) at http://gis.acgeospatial.co.uk.
Feel free to connect or follow me; I am always keen to talk about Earth Observation.