如何使用 Anaconda 的 Python 版本执行 Python 脚本?

2022-01-10 00:00:00 python python-3.x anaconda

问题描述

我最近下载了 Python 的 Anaconda 发行版.我注意到,如果我编写和执行 Python 脚本(通过双击其图标),我的计算机(在 Windows 8 上运行)将使用我的旧版本 Python 而不是 Anaconda 的版本来执行它.例如,如果我的脚本包含 import matplotlib,我会收到一个错误.有没有办法让我的脚本改用 Anaconda 的 Python 版本?

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?

我知道我可以在命令提示符下打开 Anaconda 的 Python 版本并手动导入它,但我想对我们进行设置,以便我可以双击 .py 文件和 Anaconda 的 Python 版本会自动使用.

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.


解决方案

我知道这是旧的,但是如果您希望能够双击 Python 文件并拥有正确的解释器,这里的答案都不是真正的解决方案每次您想使用不同的解释器时,无需修改 PYTHONPATHPATH 即可使用.当然,从命令行, activate my-environment 可以工作,但是 OP 专门询问了双击.

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.

在这种情况下,正确的做法是使用适用于 Windows 的 Python 启动器.然后,您所要做的就是添加 #!path ointerpreterpython.exe 到脚本的顶部.不幸的是,虽然启动器是 Python 3.3+ 的标准配置,但它不包含在 Anaconda 中(请参阅 Python & Windows:python 启动器在哪里?),最简单的做法是与 这里.

In this case, the correct thing to do is use the Python launcher for Windows. Then, all you have to do is add #! path ointerpreterpython.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.

相关文章