Wednesday, February 17, 2016

Continuous integration with Python on TeamCity

Here is how we can configure TeamCity 9.1.3 (build 37176) to perform continuous integration on Python 2 projects. I assume that you have already installed Python 2 (2.6 and above), virtual-env, TeamCity, and have access to your code in a version control system. Unit tests written in Nose seem to work well in TeamCity.

  1. Create a virtual-env on the server hosting your TeamCity installation. 
  2. Install teamcity-messages in your virtual-env.
  3. Create a new project in TeamCity. See this page for more details.
  4. Setup your VCS root(s). This may not be the best approach, but for dependent Python projects, I create VCS roots for every dependent projects so that all dependencies are satisfied within the project. You may have to define VCS roots in a parent TeamCity project if they are shared among several dependent projects.
  5. Create a build configuration.
  6. Attach relevant VCS roots in the "Version Control Settings" page.
  7. Create a new build step with "Command Line" as the runner type.
  8. Enter the script content so that you invoke your virtual-env's nosetests.exe.
  9. Add a new trigger in the Triggers page.
  10. You can manually schedule a build now or wait for TeamCity to build automatically.

Thursday, February 4, 2016

Setting up a Python development environment for ArcGIS


I use Intellij IDEA 15.0.3 with its Python plugin to development spatial analysis platforms using ESRI ArcGIS libraries. Although the ESRI licenses are not free, I have found them to be good investments given the ease of performing spatial analysis and managing GIS datasets. For this tutorial, I will use PyCharm Community Edition instead of the full-fledged Intellij IDEA. You will need to have ArcGIS installed already. If you wish, you can also install the separate 64-bit geoprocessing package. ArcGIS 10.3 uses Python 2.7.
  1. Download PyCharm IDE.
  2. Install PyCharm IDE.
  3. Start PyCharm IDE.
  4. Create New Project. If you have existing interpreters, choose the one that was installed by ArcGIS.
  5. Open File-Settings (or Ctrl+Alt+S).
  6. Click on Project Interpreter.
  7. Click on the gear icon (near the top right corner of the window).
  8. Select "Create VirtualEnv". This will give you a separate Python environment for each project.
  9. Give the virtual environment a name and location.
  10. Check "Inherit global site-packages".
  11. Click OK to clear the Settings windows.
  12. Go to the Project window.
  13. Right-click on "MyFirstArcpyProject".
  14. Select New-Python File.
  15. Call the new Python file "ExtractSpatialData.py".
  16. Go to your ArcMap or ArcCatalog's geoprocessing results frame.
  17. Right-click on the operation that you want to reuse.
  18. Click on "Copy As Python Snippet".
  19. Go to the PyCharm window.
  20. Paste the clipboard contents in ExtractSpatialData.py.
  21. Go to the top of the file.
  22. Add the line "import arcpy".
  23. Add the line "arcpy.env.workspace = <path to your datasets>".
  24. Run ExtractSpatialData.py (Ctrl+Shift+F10).
Instead of setting arcpy.env.workspace, you can also use the full path name to your input, clip, and output feature classes. If you are using extensions such as spatial analysis, you will need to checkout the license, e.g. arcpy.CheckOutExtension("Spatial"). See checkoutextension for more details.