Thursday, February 11, 2016

Easy way to run python script in Conda activated environments in Windows

Conda is a great tool to setup python and various packages/languages. It provides easy method to switch between environments, e.g., one can easily switch between python 2.7 and python 3.5 without resorting tedious hacks and tricks. However, when using it in Microsoft Windows, it is not as convenient as one wishes for.

In Windows, there are multiple ways to run a python script.
  1. You can set the path to python.exe in your PATH variable, then type "python path_to_your_script". Although you don't have to type the path to your python installation, you have to type the path to your script every time. You can use the script in the same directory as your command line environment, but this is an inconvenient restriction.
  2. In addition to setting the path to python.exe in PATH variable, one can also set the path to your scripts in PATH variable. Some distributions of python works well and you simply need to type "your_script_name" and Windows will call python.exe to run it. This did not work for me at least on Anaconda2-2.5.0-Windows-x86_64. I had to manually associate files with .py extension with python.exe installed by Anaconda. While this did work, there is a new problem - it always uses the root environment. In my case, the root environment is python 2.7. I installed python 3.5 through Conda and named it py35. After "activate py35", and run python.exe --version, it shows python 3.5. However, when I run my script, it still shows python 2.7. This is because when I type the name of the script, Windows loads the python.exe file (version 2.7 in this case) that I associated earlier. 
In order to take advantage of Conda's capability of switching environment, I tried a a slightly different approach. I created a file called run_python.bat and saved it in the same directory as my python scripts. You can save it anywhere as you like, just make sure to add the path to it in your PATH variable. Inside this file, there is only one line - @python.exe  %* Then I associated .py extension with this new batch file. Note that I did not put the path to python.exe in the batch file. It is intentional. Without an absolute path specified, Windows searches for it. As Conda switches the environment, the python.exe found by Windows will be different, which serves my purpose perfectly. By default, when you associate an file extension with a program, there is an entry created in Windows registry. Run regedit.exe, search for an entry called "run_python.bat" after you have associated the .py extension with it. There should be "shell:open:command" for this entry. The default value for command is "path_to_your_directory\run_python.bat" "%1", change it to "path_to_your_directory\run_python.bat" "%1" %* so that the arguments can be passed to your python script properly.

That's it. A simple trick that worked for me.