From the very beginning

If you are an absolute beginner to Python then this page will help you get set up, all the way from downloading Python to setting up an environment and running your first script.

And even if that first sentence didn’t make a whole lot of sense, still keep reading as I’ll explain along the way.

Step 1. Download Anaconda

Anaconda is a Python environment manager. Think of a Python environment as a place where your version of Python and all the relevant modules that you want to use are stored in a neat little package ready to use.

And think of modules as add-ons to Python that allow for additional functionality.

The main Python modules that I find useful for GIS are:

  • GDAL
  • Pandas and GeoPandas
  • Rasterio
  • Numpy
  • Matplotlib
  • Folium
  • Scipy
  • Sci-kit Learn
  • Requests, JSON and BS4

Once Anaconda is downloaded and installed (defaults should be fine, just click through), you’ll want to download a Python IDE to use.

step 2. python ide

An IDE stands for ‘Integrated Development Environment’. Like you would write text in a text editor like Word, you will write code in an IDE (or something like ArcGIS Pro, but we’ll get to that later).

For now, let’s just focus on writing code in an IDE. You can choose any IDE that you like that support Python. I like to use PyCharm but here’s a list of some common ones with links to them:

For PyCharm, download the community edition (bottom of the page) and once downloaded, run the installer and go through the defaults until it’s successfully installed.

Step 3. Setting up an environment

Now open Anaconda Prompt as an administrator (you don’t necessarily need to but it reduces likelihood of any privilege issues).

Press Enter and it should look like this:

Now it’s time to create an environment. Type in:

conda create --name geopython python=3.11

You can call it whatever you like but for this example I’ve gone with ‘geopython’ and I’ve decided to use the latest stable version of Python as of writing ‘3.11’.

Press y when prompted and then let it do its thing.

step 4. Activate the environment

Once the environment is created, you need to activate it. This just means that when in Anaconda Prompt, once you’ve ‘activated’ your environment, any future commands such as adding a module adds that module to your activated environment.

If you’ve also called your environment geopython then it’s as simple as typing in:

conda activate geopython

and pressing Enter.

Now lets install a few of the modules I mentioned previously.

Step 5. Installing modules in your environment

Now that you’ve created and activated your Conda Python Environment you could start your Python coding journey. But before you do that, you might want to add a few more modules.

Note that these modules can be added (and removed) at any time.

To add a module type:

conda install -c conda-forge gdal

Now repeat this for a few others if you like such as folium, pandas, geopandas and rasterio.

The benefit of storing these modules in an Anaconda environment is that it keeps everything nice and tidy so that you have modules ready to go for different Python projects.

For instance you could have another environment called machine_learning that has modules like scipy and sci-kit learn within.

So now that you have an environment created, it’s time to use it in your IDE.

Step 5. Using your environment in an ide

As Python 3.11 is a recent release, make sure that you have the latest stable release of your IDE downloaded. If you just downloaded it then it should be, otherwise updating it should be as simple of going to the options menu and checking for an update.

If it’s your very first time opening your IDE then it will likely prompt you to create a new project.

Type in the name of your project. I’ve called mine GeoPythonProject. This is where you’ll store all your files and scripts relevant to whatever project you’re working on.

Then click the Python Interpreter dropdown button and click the ‘Previously configured interpreter’ radio button.

The environment you created previously in Anaconda Prompt should be listed. If it is then click on it and then click the create button at the bottom of the screen. You are now good to go!

But if it isn’t then go back to Ancaconda Prompt and type in conda info –envs. This will list then name of your environment(s) and where they are stored. Copy the path of your environment and then go back to your IDE. Click ‘Add Interpreter’ then ‘Add Local Interpreter’. Click the ‘Existing’ radio button and then the 3 dots to the right of the Interpret drop down. Paste the path into the file explorer popup like this: W:\Python\Anaconda\envs\geopython and add \Python.exe to the end. If it exists then you should be able to click the OK button. If so, go back to the project creator page and create your project. You are now good to go.

Step 6. Create your first script

Right click on your project name and click New -> Python File. Call it whatever you like and now you’re ready to go! When you’re ready to run the code, click the Run tab at the top and click Run, press Alt+Shift+F10 (for PyCharm) or right click your script tab and click Run.

step 7. Using Python in ArcGIS Pro / QGIS etc

Now that you have your Python environment set up, for GIS software that supports Python, it should be as simple as pointing the software towards your PythonEnvironementName\Python.exe path.

  • ArcGIS Pro – an article I wrote from when I worked at Esri UK. See the first screenshot – click Manage Environments and create a new one with your PythonEnvironmentName\Python.exe path
  • QGIS – You can install QGIS via Anaconda like you did with the previous modules for it to use other modules within that environement. Otherwise in Plugins in an already downloaded QGIS you should see ‘Python Console’

step 8. Happy Coding!

You have successfully created a Python Environment and some relevant modules, downloaded an IDE, created a new Project and activated your environment within it, ready to use. Now it’s on to the fun part!

If you found this useful then feel free to share! If you have any questions then ask away and keep an eye out for new posts coming soon. I’ve had a little break while a work on a couple of other projects but now have lots of ideas and tutorials that I’ll be working on and posting soon.

Leave a comment

Latest Stories