問題描述
我最近下載了 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 文件并擁有正確的解釋器,這里的答案都不是真正的解決方案每次您想使用不同的解釋器時,無需修改 PYTHONPATH
或 PATH
即可使用.當然,從命令行, 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.
這篇關于如何使用 Anaconda 的 Python 版本執行 Python 腳本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!