Working with multiple Python versions

This is something I’m sure many of you already know, but it was new to me, so I figured that I’d share it here.  My student, Carl Flint, showed this to me last night.

Like many of you, I do Python programming in both ArcGIS and other open source platforms. This can sometimes be a real trick, as the ArcGIS Python is located in C:\Python27\ArcGIS10.3\……, while other Python packages are located in C:\Python27.

Yesterday in class, I wanted my students to take data from a .csv file, use geocoder to geocode locations, push the locations into PostGIS, and run a spatial query, and then run pygal to create a pie chart of the results – sort of a mini-geodashboard in the making.  That was really fun for the students to see how Python can get all these products interacting with one another.

But, my GIS Programming course also includes lessons on Arcpy, so I wanted to have them do the same thing except to use ArcGIS to perform the spatial analysis task.  The problem was, the Python libraries in Arcpy are located in a different spot than the regular Python 2.7.  And,  I was having difficulty installing pip in the ArcGIS directory.

So, Carl showed me how to use my Python27 directory to load the Python packages into the ArcGIS directory.  Under normal circumstances, you simply issue (replace psycopg2 with your favorite Python package like geocoder, pygal, etc.):

python -m pip install psycopg2 --target=C:\Python27\ArcGIS10.3\Lib\site-packages

Now, in our case there were other dependency problems, so I also had to issue the following two commands:

python -m pip install distribute --target=C:\Python27\ArcGIS10.3\Lib\site-packages
python -m pip install setuptools --target=C:\Python27\ArcGIS10.3\Lib\site-packages

That was it.  Now, I can use Arcpy to interact with all these really cool Python packages.

As I said, I know a lot of you may already know this, but I hope it helps others.

Want to learn how to program using Python for Geospatial?  Check out my Python for Geospatial course,  or any of the 9 other geospatial courses I offer on Udemy here.  The links will get you any of the courses for $30 or less.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s