courserastudent
courserastudent

Reputation: 501

How to use Anaconda Python to execute a .py file?

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?

Upvotes: 40

Views: 226397

Answers (9)

Wisarut Bholsithi
Wisarut Bholsithi

Reputation: 171

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")

Upvotes: 0

Parag Sonawane
Parag Sonawane

Reputation: 1

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.

Upvotes: 0

Atharva Rayar
Atharva Rayar

Reputation: 31

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

Upvotes: 3

aaronrums
aaronrums

Reputation: 11

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)

Upvotes: 1

Mantej Singh
Mantej Singh

Reputation: 410

You can do it from the "Anaconda Prompt"

conda run "my_script.py"

Upvotes: 1

James McIntyre
James McIntyre

Reputation: 116

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?

Upvotes: 2

user1749106
user1749106

Reputation: 41

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

Upvotes: 4

Martin Bosch
Martin Bosch

Reputation: 265

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.

Upvotes: 25

Nickpick
Nickpick

Reputation: 6587

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.

Upvotes: 0

Related Questions