简体   繁体   English

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

[英]How can I execute Python scripts using Anaconda's version of Python?

I recently downloaded the Anaconda distribution for Python.我最近下载了 Python 的 Anaconda 发行版。 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.我注意到,如果我编写和执行 Python 脚本(通过双击其图标),我的计算机(在 Windows 8 上运行)将使用我的旧版本 Python 而不是 Anaconda 的版本来执行它。 So for example, if my script contains import matplotlib , I will receive an error.例如,如果我的脚本包含import matplotlib ,我会收到一个错误。 Is there a way to get my scripts to use Anaconda's version of Python instead?有没有办法让我的脚本改用 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.我知道我可以在命令提示符下打开 Anaconda 的 Python 版本并手动导入它,但我想设置我们的东西,以便我可以双击一个 .py 文件并自动使用 Anaconda 的 Python 版本.

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.我知道这已经过时了,但是如果您希望能够双击 Python 文件并使用正确的解释器,而无需在每次想要使用不同的解释器时修改PYTHONPATHPATH ,那么这里的答案都不是真正的解决方案。 Sure, from the command line, activate my-environment works, but OP specifically asked about double-clicking.当然,从命令行activate my-environment可以工作,但是 OP 专门询问了双击。

In this case, the correct thing to do is use the Python launcher for Windows .在这种情况下,正确的做法是使用适用于 Windows 的 Python 启动器 Then, all you have to do is add #! path\to\interpreter\python.exe然后,您所要做的就是添加#! path\to\interpreter\python.exe #! path\to\interpreter\python.exe to the top of your script. #! path\to\interpreter\python.exe到脚本的顶部。 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 .不幸的是,虽然启动器是 Python 3.3+ 的标准配置,但它不包含在 Anaconda 中(请参阅Python 和 Windows:python 启动器在哪里? ),最简单的做法是从此处单独安装它。

Set your python path to the Anaconda version instead将您的 python 路径设置为 Anaconda 版本

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. Windows 有一个用于更改环境变量的内置对话框(以下指南适用于 XP 经典视图):右键单击您机器的图标(通常位于您的桌面上并称为“我的电脑”)并在此处选择属性。 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 (ie Administrator rights).要更改系统变量,您需要不受限制地访问您的机器(即管理员权限)。

Find your PATH variable and add the location of your Anaconda directory.找到您的 PATH 变量并添加您的 Anaconda 目录的位置。

Example of someone doing it here: How to add to the PYTHONPATH in Windows, so it finds my modules/packages?有人在这里做的例子: 如何在 Windows 中添加到 PYTHONPATH,以便它找到我的模块/包? Make sure that you sub path out for the Anaconda file though.不过,请确保您为 Anaconda 文件设置了子路径。

The instructions in the official Python documentation worked for me: https://docs.python.org/2/using/windows.html#executing-scripts官方 Python 文档中的说明对我有用: https ://docs.python.org/2/using/windows.html#executing-scripts

  1. Launch a command prompt.启动命令提示符。

  2. Associate the correct file group with .py scripts:将正确的文件组与 .py 脚本相关联:

     assoc .py=Python.File

Redirect all Python files to the new executable:将所有 Python 文件重定向到新的可执行文件:

    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.该示例显示了如何将 .py 扩展名与 .pyw 可执行文件相关联,但如果您想将 .py 扩展名与 Anaconda Python 可执行文件相关联,它可以工作。 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.名称“Python.File”可以是任何名称,您只需确保 ftype 命令中的名称相同。 When you finish and before you try double-clicking the .py file, you must change the "Open with" in the file properties.完成后,在尝试双击 .py 文件之前,您必须更改文件属性中的“打开方式”。 The file type will be now ".py" and it is opened with the Anaconda python.exe.文件类型现在将是“.py”,并使用 Anaconda python.exe 打开。

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.我喜欢运行 Python 2 的“基本”版本来验证我为其他人创建的脚本,而无需高级 python 设置。 But Anaconda and Python 3 have a lot of nice features.但是 Anaconda 和 Python 3 有很多不错的特性。 To enjoy both things on the same computer I do this on my Windows computer which allows me to easily switch.为了在同一台计算机上享受这两件事,我在我的 Windows 计算机上执行此操作,这让我可以轻松切换。

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欲了解更多信息: http ://conda.pydata.org/docs/test-drive.html

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.但是,将 Anaconda 添加到 PYTHONPATH 对我不起作用。 What got it fixed was the following:修复它的原因如下:

  1. Added Anaconda to the PYTHONPATH and remove any other distribution of Python from any paths.将 Anaconda 添加到 PYTHONPATH 并从任何路径中删除任何其他 Python 发行版。
  2. Opened the command prompt and started python (Here I had to verify that it was indeed running under the Anaconda dist)打开命令提示符并启动python(这里我必须验证它确实在Anaconda dist下运行)
  3. Ran the following lines inside anaconda在 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'将脚本的默认应用程序更改为“python”

After doing this, my scripts are working on double click.完成此操作后,我的脚本正在双击。

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.这个解决方案并不能完美地回答这个问题,因为它会将您的 .py 文件修改为 .bat 文件,但它可以用于保存目的,所以我认为它可以帮助少数人。

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 :要使用 anaconda 的环境启动 python 脚本:将 .py 扩展名更改为 .bat 扩展名,并在脚本开头添加此命令行:

@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.它将启动一个 .bat 文件,该文件将激活您的 conda 环境并执行您的 python 脚本。 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 :以下是“.bat”文件内容的示例:

@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 .注意:如果 anaconda 不在您的 PATH 环境变量中,只需将 : conda activate替换为 anaconda activate .bat 文件的路径,它应该看起来像这样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>您也可以将环境名称替换为它的目录,它通常看起来像这样: 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 Python 命令行 -x 选项

不知道 Windows 8,但您可能可以为特定扩展设置默认 prog,例如在 Windows 7 上,您右键单击 => 打开方式,然后选择所需的 prog 并选择“将此 prog 用作默认值”,或者您可以从路径中删除旧版本的 python 并添加 anaconda

You can try to change the default .py program via policy management.您可以尝试通过策略管理更改默认的.py程序。 Go to windows, search for regedit, right click it.转到windows,搜索regedit,右键单击它。 And then run as administrator.然后以管理员身份运行。 Then, you can search the key word "python.exe" And change your Python27 path to you Anaconda path.然后,您可以搜索关键字“python.exe”并将您的 Python27 路径更改为您的 Anaconda 路径。

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.这是我在特定的anaconda环境中双击启动python脚本的方法。

I have found a very useful script in the anaconda3 folder cwp.py .我在 anaconda3 文件夹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:所以实际上,假设你的 anaconda 安装在C:\Users\User\anaconda3并且你的环境在C:\Users\User\anaconda3\env\myenv那么你所要做的就是在一个窗口中写下以下行快捷方式作为目标:

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!双击快捷方式将启动您的脚本!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM