34

I recently downloaded the Anaconda distribution for Python. I noticed that if I write and execute a Python script (by double-clicking on its icon), my computer (running on Windows 8) will execute it using my old version of Python rather than Anaconda's version. So for example, if my script contains import matplotlib, I will receive an error. Is there a way to get my scripts to use Anaconda's version of Python instead?

I know that I can just open Anaconda's version of Python in the command prompt and manually import it, but I'd like to set things us so that I can just double-click on a .py file and Anaconda's version of Python is automatically used.

Alessandro Power
  • 2,395
  • 2
  • 19
  • 39

9 Answers9

13

I know this is old, but none of the answers here is a real solution if you want to be able to double-click Python files and have the correct interpreter used without modifying your PYTHONPATH or PATH every time you want to use a different interpreter. Sure, from the command line, activate my-environment works, but OP specifically asked about double-clicking.

In this case, the correct thing to do is use the Python launcher for Windows. Then, all you have to do is add #! path\to\interpreter\python.exe to the top of your script. Unfortunately, although the launcher comes standard with Python 3.3+, it is not included with Anaconda (see Python & Windows: Where is the python launcher?), and the simplest thing to do is to install it separately from here.

Community
  • 1
  • 1
JaminSore
  • 3,758
  • 1
  • 25
  • 21
9

Set your python path to the Anaconda version instead

Windows has a built-in dialog for changing environment variables (following guide applies to XP classical view): Right-click the icon for your machine (usually located on your Desktop and called “My Computer”) and choose Properties there. Then, open the Advanced tab and click the Environment Variables button.

In short, your path is:

My Computer ‣ Properties ‣ Advanced ‣ Environment Variables In this dialog, you can add or modify User and System variables. To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).

Find your PATH variable and add the location of your Anaconda directory.

Example of someone doing it here: How to add to the PYTHONPATH in Windows, so it finds my modules/packages? Make sure that you sub path out for the Anaconda file though.

marsh
  • 2,592
  • 5
  • 29
  • 53
  • I actually did this as well. The good news is that when I type "python" into my command prompt, the Anaconda version of Python appears. The bad news is that double-clicking on a .py file still brings up the old version of Python. – Alessandro Power Nov 18 '14 at 22:53
  • 1
    cause the default application for .py is your old python, change the default application to anaconda version of python and restart you pc, it should work – Ludovic Viaud Nov 18 '14 at 22:56
  • 1
    I did in fact change the default application for .py, and even restarted my computer. I did this on top of creating a new PythonPath variable in my environment variables. It unfortunately did not work. – Alessandro Power Nov 18 '14 at 23:27
  • @Marsh: Okay, so some small progress has been made. I'm not sure if this was implied in your post, but I decided to add the path to Anaconda's libraries in PythonPath instead of the path to Anaconda's python.exe file. Specifically, I created a PythonPath variable with variable value C:\Users\Alessandro\Anaconda3\Lib\site-packages\. I'm still getting errors, but the errors are different now, which I guess is progress. When I try to run my script, it says: ImportError: DLL load failed: %1 is not a valid Win32 application. (with some other stuff before that which won't fit in this comment). – Alessandro Power Nov 18 '14 at 23:54
  • Yes that is what I meant. Try changing just the PythonPath variable to the Anaconda exe then add a path the Anaconda libraries and the python libraries so it can find everything. – marsh Nov 19 '14 at 12:49
  • Is there a missing word in "Find your PATH variable and it to the location of your Anaconda directory." ? I suppose it should say "... and *add* it to the location ..." but I want to know the answer for sure. – Winston Smith Feb 19 '20 at 22:08
7

The instructions in the official Python documentation worked for me: https://docs.python.org/2/using/windows.html#executing-scripts

  1. Launch a command prompt.

  2. Associate the correct file group with .py scripts:

    assoc .py=Python.File
    

Redirect all Python files to the new executable:

    ftype Python.File=C:\Path\to\pythonw.exe "%1" %*

The example shows how to associate the .py extension with the .pyw executable, but it works if you want to associate the .py extension with the Anaconda Python executable. You need administrative rights. The name "Python.File" could be anything, you just have to make sure is the same name in the ftype command. When you finish and before you try double-clicking the .py file, you must change the "Open with" in the file properties. The file type will be now ".py" and it is opened with the Anaconda python.exe.

Ernesto561
  • 557
  • 1
  • 6
  • 11
  • 1
    Worked great, thanks. FYI, if you receive an "Access Denied" error when trying to use these commands, open the command prompt "As Administrator" and try again. This will be necessary if you have UAC enabled. – Mike Clark May 08 '19 at 22:32
7

I like to run a "bare-bones" version of Python 2 to verify scripts that I create for other people without an advanced python setup. But Anaconda and Python 3 have a lot of nice features. To enjoy both things on the same computer I do this on my Windows computer which allows me to easily switch.

C:\Users>python --version
Python 2.7.11

C:\Users>conda create --name p3 python=3

C:\Users>conda info --envs
Using Anaconda Cloud api site https://api.anaconda.org
# conda environments:
#
p3                       C:\Anaconda3\envs\p3
root                  *  C:\Anaconda3

C:\Users>activate p3
Deactivating environment "C:\Anaconda3"...
Activating environment "C:\Anaconda3\envs\p3"...

[p3] C:\Users>python --version
Python 3.5.1 :: Continuum Analytics, Inc.

For more info: http://conda.pydata.org/docs/test-drive.html

sparrow
  • 10,794
  • 12
  • 54
  • 74
4

I know this is an old post, but I recently came across with the same problem. However, adding Anaconda to PYTHONPATH wasn't working for me. What got it fixed was the following:

  1. Added Anaconda to the PYTHONPATH and remove any other distribution of Python from any paths.
  2. Opened the command prompt and started python (Here I had to verify that it was indeed running under the Anaconda dist)
  3. Ran the following lines inside anaconda

    >>> import sys
    >>> sys.path
    ['','C:\\Anaconda','C:\\Anaconda\\Scripts','C:\\Anaconda\\python27.zip','C:\\Anaconda\\DLLs','C:\\Anaconda\\lib','C:\\Anaconda\\lib\\plat-win','C:\\Anaconda\\lib\\lib-tk','C:\\Anaconda\\lib\\site-packages','C:\\Anaconda\\lib\\site-packages\\PIL','C:\\Anaconda\\lib\\site-packages\\Sphinx-1.2.3-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\win32', 'C:\\Anaconda\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda\\lib\\site-packages\\Pythonwin','C:\\Anaconda\\lib\\site-packages\\runipy-0.1.1-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\setuptools-5.8-py2.7.egg']
    
  4. Copied the displayed path

  5. Within the script that I'm trying to execute on double click, changed the path to the previously copied one.

    import sys
    sys.path =['','C:\\Anaconda','C:\\Anaconda\\Scripts','C:\\Anaconda\\python27.zip','C:\\Anaconda\\DLLs','C:\\Anaconda\\lib','C:\\Anaconda\\lib\\plat-win','C:\\Anaconda\\lib\\lib-tk','C:\\Anaconda\\lib\\site-packages','C:\\Anaconda\\lib\\site-packages\\PIL','C:\\Anaconda\\lib\\site-packages\\Sphinx-1.2.3-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\win32', 'C:\\Anaconda\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda\\lib\\site-packages\\Pythonwin','C:\\Anaconda\\lib\\site-packages\\runipy-0.1.1-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\setuptools-5.8-py2.7.egg']
    
  6. Changed the default application for the script to 'python'

After doing this, my scripts are working on double click.

1

This solution is not perfectly answering the question because it modifies your .py file into a .bat file but it serves the save purpose, so I think it can help few people out there.

To launch a python script with anaconda's environment : change your .py extension into a .bat extension and add this command line in the beginning of your script :

@echo off & call conda activate <env name> & python -x "%~f0" %* & goto :eof

It will launch a .bat file that will activate your conda environment and execute your python script. This allows you to have a single file that you can copy/paste anywhere and just double click on it to launch it.

Here is an example of the content of the ".bat" file :

@echo off & call conda activate my_environment & python -x "%~f0" %* & goto :eof
print("Hello World!")
input()
import pandas as pd
print('succes')
input()

Hope this helps some people ! Cheers


Note: If anaconda is not in yourr PATH environment variable, just replace : conda activate with the path of anaconda activate .bat file, it should look like this C:\ProgramData\Anaconda3\Scripts\activate.bat.

You can also replace the environment name with it's directory, it often looks like that : C:\Users\<user name>\.conda\envs\<environment name>


Many thanks to this post that helped me figure out this solution:

Python command line -x option

Ash
  • 21
  • 2
  • I don't have @Ash's obvious expertise. But another thing that might be helpful is to look at the properties of the Windows Start menu's Spyder shortcut. The Target field on my Win10 PC contains the following text string: "C:\ProgramData\Anaconda3\pythonw.exe C:\ProgramData\Anaconda3\cwp.py C:\ProgramData\Anaconda3 C:\ProgramData\Anaconda3\pythonw.exe C:\ProgramData\Anaconda3\Scripts\spyder-script.py" I'd love to hear what more knowledgeable people think about the idea of emulating that pattern to run other python scripts from the Windows cmd line. – Marc B. Hankin Aug 12 '21 at 14:57
0

don't know windows 8 but you can probably set the default prog for a specific extension, for example on windows 7 you do right click => open with, then you select the prog you want and select 'use this prog as default', or you can remove your old version of python from your path and add the one of the anaconda

Ludovic Viaud
  • 202
  • 1
  • 5
0

You can try to change the default .py program via policy management. Go to windows, search for regedit, right click it. And then run as administrator. Then, you can search the key word "python.exe" And change your Python27 path to you Anaconda path.

ZYJ
  • 57
  • 6
0

I know that this is an old question, but still there is no accepted answer.

Here is what I'm doing to start a python script with a double click in a specific anaconda environment.

I have found a very useful script in the anaconda3 folder cwp.py. This doesn't activate an environment, but it adds the environment prefix to the path.

You should use it as follow:

python cwp.py PREFIX ARGS

So actually, assuming that your anaconda installation is in C:\Users\User\anaconda3 and your environment is in C:\Users\User\anaconda3\env\myenv then what you have to do is to write the following line in a windows shortcut as a target:

C:\Users\User\anaconda3\pythonw.exe C:\Users\User\anaconda3\cwp.py C:\Users\User\anaconda3\env\myenv C:\Users\User\anaconda3\env\myenv\pythonw.exe path_to_your_script\your-script.py

A double click on the shortcut will start your script!

toto
  • 110
  • 1
  • 6