Python 如何在虚拟环境中运行 Spyder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30170468/
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
How to run Spyder in virtual environment?
提问by tomaskazemekas
I have been using Spyder installed with with Anaconda distribution which uses Python 2.7 as default. Currently I need to set up a development virtual environment with Python 3.4.
我一直在使用安装了 Anaconda 发行版的 Spyder,该发行版默认使用 Python 2.7。目前我需要使用 Python 3.4 设置开发虚拟环境。
Top two suggestions after research online are:
在线研究后的前两个建议是:
- to set up virtual environment first and to point change the preferences of Spyder , e.g here;
- to install all Spyder dependencies, like PyQt4, in the virtual environment itself, e. g. here;
Both recommendations are cumbersome and do not look like smart options for development.
这两个建议都很麻烦,看起来不像是开发的明智选择。
Is there a solution that would allow to run Spyder with required Python version automatically after activating the required virtual environment?
是否有解决方案可以在激活所需的虚拟环境后自动运行具有所需 Python 版本的 Spyder?
采纳答案by tomaskazemekas
There is an option to create virtual environments in Anacondawith required Python version.
可以选择使用所需的 Python 版本在 Anaconda 中创建虚拟环境。
conda create -n myenv python=3.4
To activate it :
要激活它:
source activate myenv # (in linux, you can use . as a shortcut for "source")
activate myenv # (in windows - note that you should be in your c:\anaconda2 directory)
UPDATE. I have tested it with Ubuntu 18.04. Now you have to install spyder additionally for the new environment with this command (after the activation of the environment with the command above):
更新。我已经用 Ubuntu 18.04 对其进行了测试。现在您必须使用此命令为新环境另外安装 spyder(在使用上述命令激活环境后):
conda install spyder
(I have also tested the installation with pip, but for Python 3.4 or older versions, it breaks with the library dependencies error that requires manual installation.)
(我也用 pip 测试了安装,但是对于 Python 3.4 或更旧的版本,它会因需要手动安装的库依赖项错误而中断。)
And now to run Spyder with Python 3.4 just type:
现在要使用 Python 3.4 运行 Spyder,只需键入:
spyder
回答by Tin Luu
Additional to tomaskazemekas's answer: you should install spyder in that virtual environment by:
除了 tomaskazemekas 的回答:您应该通过以下方式在该虚拟环境中安装 spyder:
conda install -n myenv spyder
(on Windows, for Linux or MacOS, you can search for similar commands)
(在 Windows 上,对于 Linux 或 MacOS,您可以搜索类似的命令)
回答by Jerusalem Programmer
I just had the same problem trying to get Spyder to run in Virtual Environment.
我只是在尝试让 Spyder 在虚拟环境中运行时遇到了同样的问题。
The solution is simple:
解决方法很简单:
Activate your virtual environment.
激活您的虚拟环境。
Then pip install Spyder and its dependencies (PyQt5) in your virtual environment.
然后在您的虚拟环境中 pip install Spyder 及其依赖项 (PyQt5)。
Then launch Spyder3 from your virtual environment CLI.
然后从您的虚拟环境 CLI 启动 Spyder3。
It works fine for me now.
现在对我来说很好用。
回答by Steven Kalt
The above answers are correct but I calling spyder
within my virtualenv would still use my PATH
to look up the version of spyder in my default anaconda env. I found this answerwhich gave the following workaround:
上面的答案是正确的,但我spyder
在我的 virtualenv 中调用仍然会使用 myPATH
在我的默认 anaconda env 中查找 spyder 的版本。我找到了这个答案,它提供了以下解决方法:
source activate my_env # activate your target env with spyder installed
conda info -e # look up the directory of your conda env
find /path/to/my/env -name spyder # search for the spyder executable in your env
/path/to/my/env/then/to/spyder # run that executable directly
I chose this over modifying PATH
or adding a link to the executable at a higher priority in PATH
since I felt this was less likely to break other programs. However, I did add an alias to the executable in ~/.bash_aliases
.
我选择了这个而不是修改PATH
或添加指向更高优先级的可执行文件的链接,PATH
因为我觉得这不太可能破坏其他程序。但是,我确实在~/.bash_aliases
.
回答by Boussad
What worked for me :
什么对我有用:
- run spyder from the environment (after source activate)
- go to Tools --> preferences --> python Interpreter and select the python file from the env you want to link to
spyder
ex : /home/you/anaconda3/envs/your_env/bin/python
- 从环境运行 spyder(源激活后)
- 转到工具 --> 首选项 --> python 解释器并从要链接到
spyder
ex 的环境中选择 python 文件:/home/you/anaconda3/envs/your_env/bin/python
Worked on ubuntu 16, spyder3, python3.6.
曾在 ubuntu 16、spyder3、python3.6 上工作。
回答by adelriosantiago
Here is a quick way to do it in 2020. Using the Anaconda Navigator:
这是在 2020 年实现的快速方法。使用 Anaconda Navigator:
- Open Anaconda Navigator
- Create your new environment as you wish. I named this environment "test". Click on it to activate it.
- 打开 Anaconda 导航器
- 根据需要创建新环境。我将此环境命名为“测试”。单击它以激活它。
- Go to "Home" and click on "Install" under the Spyder box.
- 转到“主页”并单击 Spyder 框下的“安装”。
- Click "Launch/Run"
- 单击“启动/运行”
There are still some minor bugs when setting up your environment (most of which are solved by restarting the Navigator), if you find a bug, please post it in Anaconda Issuesbug-tracker. Thanks.
设置环境时还有一些小bug(大部分可以通过重启导航器解决),如果你发现了bug,请在Anaconda Issuesbug-tracker中发布。谢谢。
If even then this doesn't work, Navigator could still be useful, clicking on >Enviroments takes you to a management window for the modules installed on such enviroment, searching and selecting the spyder related ones, and then clicking on Apply will install them and their dependencies.
如果即使这样也行不通,导航器仍然有用,单击 >Enviroments 会将您带到安装在此类环境中的模块的管理窗口,搜索并选择与 spyder 相关的模块,然后单击应用将安装它们并他们的依赖。
回答by smoothware
On Windows:
在 Windows 上:
You can create a shortcut executing
您可以创建一个快捷方式执行
Anaconda3\pythonw.exe Anaconda3\cwp.py Anaconda3\envs\<your_env> Anaconda3\envs\<your env>\pythonw.exe Anaconda3\envs\<your_env>\Scripts\spyder-script.py
However, if you started spyder from your venv inside Anaconda shell I believe it creates this shortcut for you (just search "Spyder" on Windows).
但是,如果您从 Anaconda shell 中的 venv 启动 spyder,我相信它会为您创建此快捷方式(只需在 Windows 上搜索“Spyder”)。
回答by Alexis
To do without reinstalling spyder in all environments follow official reference here.
要在所有环境中不重新安装 spyder,请遵循此处的官方参考。
In summary (tested with conda):
总之(用 conda 测试):
- Spyder should be installed in the base environment
- Spyder 应该安装在基础环境中
From the system prompt:
从系统提示:
Create an new environment. Note that depending on how you create it (conda, virtualenv) the environment folder will be located at different place on your system)
Activate the environment (e.g.,
conda activate [yourEnvName]
)Install spyder-kernels inside the environment (e.g.,
conda install spyder-kernels
)Find and copy the path for the python executable inside the environment. Finding this path can be done using from the prompt this command
python -c "import sys; print(sys.executable)
Deactivate the environment (i.e., return to base
conda deactivate
)run spyder (
spyder
)Finally in spyder menu go to references > Python Interpreter > Use the following interpreter and paste the environment python executable path
Restart the ipython console
创造一个新的环境。请注意,根据您创建它的方式(conda、virtualenv),环境文件夹将位于系统上的不同位置)
激活环境(例如,
conda activate [yourEnvName]
)在环境中安装 spyder-kernels(例如,
conda install spyder-kernels
)在环境中查找并复制 python 可执行文件的路径。可以在提示符下使用此命令找到此路径
python -c "import sys; print(sys.executable)
停用环境(即返回 base
conda deactivate
)运行 spyder (
spyder
)最后在 spyder 菜单中转到参考 > Python 解释器 > 使用以下解释器并粘贴环境 python 可执行路径
重启 ipython 控制台
PS: in spyder you should see at the bottom something like this
Voila
瞧