Python for Geospatial work flows, part 3: Virtual Environments
https://unsplash.com/photos/wgivdx9dBdQ

Python for Geospatial work flows, part 3: Virtual Environments

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 Vnvironment?

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:

python

and the Python environment will be working. Test by importing one of the standard libraries, such as 'os'.

import os

If all is working then no errors should be returned.

type:

exit()

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:

deactivate

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 environment in Anaconda.

Recap

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 rnvironment 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, leave them below or get in contact with me on my website.

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.

I have grouped all my previous blogs (technical stuff / tutorials / opinions / ideas) at https://gis.acgeospatial.co.uk.

Feel free to connect or follow me; I am always keen to talk about Earth Observation.

I am @map_andrew on twitter


image credit https://unsplash.com/photos/wgivdx9dBdQ

Martin Morlot

PhD student researcher in civil / environmental engineering

5 年

Why do people insist on using Windows instead of Linux for these purposes.

回复
Swaroop Humane, PMP?

Project Manager || PMP? || CSM? || SAFe? 6.0 Agilist || Prince2? || ITIL?|| Agile Scrum Master

5 年

Great Tutorials, appreciate your effort. Looking forward for many more useful GIS related Videos.????

Vincent Goffin

Data and Cloud Engineer | Data, GIS & Science Developer | MSc. in Geological and Mining Engineering [he/him]

5 年

Great articles, clear and concise, thanks a lot! What would you say about using docker containers to achieve the same thing ?

Rob van Putten

senior specialist flood protection and geotechnics | developer | trainer | innovator

5 年

Next step, dockers.. really, really nice!

要查看或添加评论,请登录

Andrew Cutts的更多文章

  • Sentinel-5P and xarray

    Sentinel-5P and xarray

    I've written about Sentinel-5P several times in the past. You can check these posts out below: The Sentinel-5P and…

    1 条评论
  • Ridge map plots using Python

    Ridge map plots using Python

    I saw this tweet on 1st May: I've seen these plots created before and find them visually very pleasing. I had a look at…

    4 条评论
  • sentinelsat - using Python to search and download Sentinel 2 data

    sentinelsat - using Python to search and download Sentinel 2 data

    There are so many ways to download Sentinel 2 data. I am not going to list them all here, but most involve an…

    3 条评论
  • Earth Observation Q1 2019 review

    Earth Observation Q1 2019 review

    I started writing these reviews two years ago, really to get a feel of the pace of change in Earth Observation /…

    1 条评论
  • Running a script via a GUI in Python, part 1

    Running a script via a GUI in Python, part 1

    I wanted to put a brief guide together to show how simple it can be to run a script in Python via a Graphical User…

    5 条评论
  • Julia for Geospatial part 3 - reading, displaying and writing raster data

    Julia for Geospatial part 3 - reading, displaying and writing raster data

    This is part three of the Julia for Geospatial series. In part one, available here, I introduced Julia and showed you…

  • Julia for Geospatial part 2 - speed test

    Julia for Geospatial part 2 - speed test

    Last time, I introduced Julia and walked through some basic instructions on how to setup. If you missed it go here.

    6 条评论
  • Julia for Geospatial Programming part 1 - setting up

    Julia for Geospatial Programming part 1 - setting up

    The top six programming languages for Data Science, according to the first links returned to me from google are: Python…

    2 条评论
  • Google Earth Engine: Sentinel 5P, and the demise of Fusion Tables

    Google Earth Engine: Sentinel 5P, and the demise of Fusion Tables

    Google Earth Engine seems to be updated pretty frequently. Towards the end of 2018, I noticed two interesting things -…

    3 条评论
  • Earth Observation Q4 2018 review

    Earth Observation Q4 2018 review

    I started writing these reviews in 2017, really to get a feel of the pace of change in Earth Observation / Geospatial…

社区洞察

其他会员也浏览了