40

I just downloaded and installed Anaconda on my Windows computer. However, I am having trouble executing .py files using the command prompt. How can I get my computer to understand that the python.exe application is in the Anaconda folder so it can execute my .py files?

0

9 Answers 9

25

You should use Anaconda Prompt instead of common Windows command prompt. Then navigate to your folder with the .py file and run:

 python myfile.py

However if you want to use normal command prompt you should put the path with you're python.exe which is usually in

C:\Users\<username>\AppData\Local\Continuum\anaconda3\python.exe

behind this one put your .py file.

Sign up to request clarification or add additional context in comments.

3 Comments

Launching just python.exe directly without activating a conda environment is limited though, for instance it won't allow importing numpy because the PATH isn't setup correctly and numpy won't find its dependencies. conda run though does this exactly, e.g. c:\tools\miniconda3\Scripts\conda.exe run -n base -v python -c "import numpy" will work.
Though conda run seems a bit broken in general (see e.g. github.com/conda/conda/issues/2904), so using a script which first has call /path/to/condabin/conda.bat activate, then python myscript.py is still the proper solution, afaik.
Windows 10: C:\Users\<username>\anaconda3\python.exe
4

Launch JupyterLab from Anaconda (Perform the following operation with JupyterLab ...)

Click on icon folder in side menu

Start up "Text File"

Rename untitle.txt to untitle.py (The name of the file started up was also changed)

Start up the "terminal" (In windows the power shell starts up)

Execute the command python untitle.py

Comments

3

Just get to the home of jupyter notebook and select "New" then select "Text file".

Then save the text file as file_name.py

Write your code in the file and save the file.

Then open the "Anaconda Prompt" and then type as follows to run your file

python file_name.py

Comments

2

Right click on a .py file and choose 'open with'

Scroll down through the list of applications and click something like 'use a different program'

Naviage to C:\Users\<username>\AppData\Local\Continuum\anaconda3

click on python.exe and then click on 'ok' or 'open'

Now when you double click on any .py file it will run it through Anaconda's interpreter and therefore run the python code.

I presume if you run it through the command line the same would apply but perhaps someone could correct me?

1 Comment

In my case it worked with this path: C:\Users\<username>\Anaconda3 :: I didn't have the ...Continuum\anaconda3 folder.
1

You can do it from the "Anaconda Prompt"

conda run "my_script.py"

2 Comments

If you're already in an Anaconda prompt there's no reason to use conda run, just use python my_script.py
Oh, yea that works toom thanks for sharing. Earlier I used to do 'conda run' but now I use python my_script.py
1

I was doing exactly as Martin Bosch suggested, and was getting the following:

(base) C:\>python command.py
python: can't open file 'command.py': [Errno 2] No such file or directory

I solved it this way:

navigate to the exact file location using the "cd" command

for me this was:

(base) C:\>cd my_scripts

this should put you specifically in the file where your .py script is located.
now you should try to input the name of your file.

(base) C:\my_scripts> test_script.py

you may get asked which program to run this with, and simply find python.exe

After doing this process once, I can simply type (in anaconda prompt)

test_script.py

and it runs no problem, even from the top of the file tree (I don't have to be in the exact file, nor do I have to explicitly give the whole file path)

2 Comments

You have to add folder C:\my_scripts to system PATH environment variable in order to find python scripts without navigating to the exact location.
"then navigate to your folder with the .py file" is exactly what you did. The rest of your procedure is not recommended. Better specify the command with which you want to run your program.
0

Anaconda should add itself to the PATH variable so you can start any .py file with "python yourpythonfile.py" and it should work from any folder.

Alternatively download pycharm community edition, open your python file there and run it. Make sure to have python.exe added as interpreter in the settings.

3 Comments

I tried to run a .py file again and a window opened asking how I wanted to run the file. I selected python.exe but nothing worked.
'nothing worked' is not a very precise description of your problem. Try to be more specific.
@courserastudent, how did you select python.exe? Don't "look for another app" and navigate to the folder to select "python.exe". That creates a file association that's only good for opening a data file, not scripts. I've never installed Anaconda, so I don't know what file associations it creates for a standard installation, but the standard Python installation creates a Python.File progid, which should be displayed in the "open with" list as "Python" (with the python snakes icon and maybe a rocket if it's the py.exe launcher).
0

If you get the following error:

can't open file 'command.py': [Errno 2] No such file or directory

Then follow this steps to fix it:

  1. Check that you are in the correct directory where the Python file is.

  2. If you are not in the correct directory, then change the current working directory with cd path. For instance: cd F:\COURSE\Files.

  3. Now that you are in the directory where your .py file is, run it with the command python app.py.

Comments

0

Check where is the directory for the ananconda environment directory which is generally

"C:\Users\[UserName]\.conda\envs\[conda environment directory]"

You will see python.exe in that directory.

After that, you need to use the following command to execute your python file (i.e. xx.py) when you are running Anaconda prompt and you will be done:

"C:\Users\[UserName]\.conda\envs\[conda environment directory]\python.exe" xxx.py 

BTW, if you have global variable (i.g. variable yyy) that contain directory, you have to define the global variable that contains full path of directory just below the header (the import section) to prevent the "name 'yyy' is not defined" error to occur:

from pathlib import Path # dealing with path issue 
yyy = Path("[DriverLettter]:\Full\Path\of\Directory")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.