如何使用 Anaconda Python 执行 .py 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39995380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:02:38  来源:igfitidea点击:

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

pythonwindowsanaconda

提问by courserastudent

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?

我刚刚在我的 Windows 计算机上下载并安装了 Anaconda。但是,我在使用命令提示符执行 .py 文件时遇到问题。如何让我的计算机了解 python.exe 应用程序位于 Anaconda 文件夹中,以便它可以执行我的 .py 文件?

回答by Martin Bosch

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

您应该使用 Anaconda Prompt 而不是常见的 Windows 命令提示符。然后使用 .py 文件导航到您的文件夹并运行:

 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

但是,如果你想使用普通的命令提示符,你应该把你的路径放在 python.exe 中,它通常在

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

behind this one put your .py file.

在这个后面放你的 .py 文件。

回答by user1749106

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

从 Anaconda 启动 JupyterLab(使用 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)

将untitle.txt重命名为untitle.py(启动的文件名也改了)

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

启动“终端”(在 Windows 中,power shell 启动)

Execute the command python untitle.py

执行命令 python untitle.py

回答by James McIntyre

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

右键单击 .py 文件并选择“打开方式”

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

向下滚动应用程序列表,然后单击“使用其他程序”之类的内容

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

导航到 C:\Users\<username>\AppData\Local\Continuum\anaconda3

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

单击 python.exe,然后单击“确定”或“打开”

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

现在,当您双击任何 .py 文件时,它将通过 Anaconda 的解释器运行它,从而运行 python 代码。

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

我想如果您通过命令行运行它,同样适用,但也许有人可以纠正我?

回答by Mantej Singh

You can do it from the "Anaconda Prompt"

您可以从“Anaconda Prompt”中执行此操作

conda run "my_script.py"

回答by aaronrums

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

我完全按照 Martin Bosch 的建议做,并得到以下结果:

(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

使用“cd”命令导航到确切的文件位置

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.

这应该把你放在你的 .py 脚本所在的文件中。
现在您应该尝试输入文件名。

(base) C:\my_scripts> test_script.py

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

你可能会被问到用哪个程序运行它,只需找到 python.exe

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

完成此过程一次后,我可以简单地输入(在 anaconda 提示符下)

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)

它运行没有问题,即使从文件树的顶部(我不必在确切的文件中,也不必明确给出整个文件路径)

回答by Nickpick

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.

Anaconda 应该将自己添加到 PATH 变量中,以便您可以使用“python yourpythonfile.py”启动任何 .py 文件,并且它应该可以在任何文件夹中工作。

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.

或者下载 pycharm 社区版,在那里打开你的 python 文件并运行它。确保在设置中将 python.exe 添加为解释器。