如何在虚拟环境中运行 Spyder?

How to run Spyder in virtual environment?

我一直在使用安装了 Anaconda 发行版的 Spyder,它默认使用 Python 2.7。目前我需要使用 Python 3.4 设置开发虚拟环境。

网上研究后的前两个建议是:

  1. 先设置虚拟环境,然后点更改Spyder的首选项,例如
  2. 在虚拟环境本身中安装所有 Spyder 依赖项,如 PyQt4,e。 G。 ;

这两个建议都很麻烦,看起来不像开发的明智选择。

是否有一种解决方案可以在激活所需的虚拟环境后自动使用所需的 Python 版本 运行 Spyder?

有一个选项 create virtual environments in Anaconda 需要 Python 版本。

conda create -n myenv python=3.4

激活它:

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)

更新。我用 Ubuntu 18.04 测试过。现在您必须使用此命令为新环境额外安装 spyder(在使用上述命令激活环境后):

conda install spyder

(我也用pip测试过安装,但是对于Python3.4或更早的版本,它会因库依赖错误而中断,需要手动安装。)

现在 运行 Spyder with Python 3.4 只需输入:

spyder

编辑 reader:

要正常打开,使用“Anaconda Prompt” > activate myenv > spyder(然后“Anaconda Prompt”必须保持打开状态,不能用于其他命令,强制-关闭将关闭 Spyder)。这当然比长时间加载“Anaconda Navigator”>切换环境>启动Spyder(@adelriosantiago 的回答)要快。

除了 tomaskazemekas 的回答:您应该通过以下方式在该虚拟环境中安装 spyder:

conda install -n myenv spyder

(在 Windows 上,对于 Linux 或 MacOS,您可以搜索类似的命令)

我在虚拟环境中试图让 Spyder 进入 运行 时遇到了同样的问题。

解决方法很简单:

激活您的虚拟环境。

然后在您的虚拟环境中 pip 安装 Spyder 及其依赖项 (PyQt5)。

然后从您的虚拟环境 CLI 启动 Spyder3。

现在对我来说效果很好。

以上答案是正确的,但我在我的 virtualenv 中调用 spyder 仍然会使用我的 PATH 在我的默认 anaconda 环境中查找 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

我选择这个而不是修改 PATH 或在 PATH 中以更高的优先级向可执行文件添加 link,因为我觉得这不太可能破坏其他程序。但是,我确实在 ~/.bash_aliases.

中为可执行文件添加了一个别名

对我有用的:

  1. 运行 来自环境的 spyder(激活源后)
  2. 转到工具 --> 首选项 --> python 解释器和 select 环境中的 python 文件 link 到 spyder 例如:/home/you/anaconda3/envs/your_env/bin/python

致力于 ubuntu 16,spyder3,python3.6。

这是在 2021 年使用 Anaconda Navigator 快速完成此操作的方法。这是最可靠的方法,除非您想以编程方式创建环境,我认为大多数用户都不会这样做:

  1. 打开 Anaconda Navigator。
  2. 单击“环境”>“创建”并为您的环境命名。如果需要,请务必更改 Python/R 内核版本。

  1. 转到“主页”并单击 Spyder 框下的“安装”。

  1. 点击“Launch/Run”

设置环境时仍然存在一些小错误,大多数应该通过重新启动 Navigator 来解决。

如果您发现错误,也请帮助我们将其发布到 Anaconda Issues 错误跟踪器中!如果您 运行 在创建环境时遇到麻烦,或者如果环境创建不正确,您可以仔细检查已安装的内容:单击“环境”打开管理 window 显示已安装的包。搜索和 select Spyder 相关的软件包,然后单击“应用”进行安装。

在 Windows:

您可以创建快捷方式执行

Anaconda3\pythonw.exe Anaconda3\cwp.py Anaconda3\envs\<your_env> Anaconda3\envs\<your env>\pythonw.exe Anaconda3\envs\<your_env>\Scripts\spyder-script.py

但是,如果您从 Anaconda shell 中的 venv 启动 spyder,它会在 Windows 菜单中自动为您创建此快捷方式。步骤:

  1. 使用此处其他答案中提到的方法在您的 venv 中安装 spyder。

  2. (在蟒蛇中:)activate testenv虽然在我的情况下,不需要这一步。

  3. 查找windows菜单“最近添加”或在windows菜单中搜索“spyder”,找到spyder (testenv)

  • [将其添加到任务栏] 和/或

  • [查找文件源位置]并将其复制到您的桌面,例如来自 C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (64-bit),其中可以找到我的任何环境的 spyder 链接。

现在可以直接通过快捷方式启动spyder,无需打开anaconda prompt

要在所有环境中不重新安装 spyder,请遵循官方参考 here

总结(使用 conda 测试):

  • 基础环境要安装Spyder

来自系统提示:

  • 创建新环境。请注意,根据您创建它的方式(conda、virtualenv),环境文件夹将位于系统上的不同位置)

  • 激活环境(例如,conda activate [yourEnvName]

  • 在环境中安装 spyder-kernels(例如,conda install spyder-kernels

  • 在环境中查找并复制 python 可执行文件的路径。可以在提示符下使用此命令 python -c "import sys; print(sys.executable)"

  • 来查找此路径
  • 停用环境(即 return 到基础 conda deactivate

  • 运行 间谍 (spyder3)

  • 最后在 spyder 工具菜单中转到 Preferences > Python Interpreter > Use the following interpreter and paste the environment python executable path

  • 重新启动 ipython 控制台

PS: 在 spyder 中你应该在底部看到这样的东西

我遵循了上面的建议之一,并且确实有效。总之,当您在 Ubuntu 上下载 Anaconda 时,使用上面给出的建议可以帮助您进入 'create' 环境。在我的例子中,你下载 Spyder 时的默认设置是:(base) smith@ubuntu ~$。创建环境后,即 fenics 并使用 $ conda activate fenics 激活它,提示更改为 (fenics) smith@ubuntu ~$.然后你从这个提示启动 Spyder,即 $ spyder 并且你的系统打开 Spyder IDE,你可以在上面编写 fenics 代码。请记住,每次打开终端时,系统都会打开默认提示。您必须激活您的包裹所在的环境并及时更改它,即(fenics)。

来自 Github 上的 Spyder 官方页面:

The naive approach

To use Spyder with another environment, the simplest way is to just install it directly into the environment from which you'd like to use the packages in, and run it from there. This works with all Spyder versions and should require no extra configuration once the IDE is installed; however, it results in multiple installations to manage and isn't as flexible or configurable as the alternative. Therefore, when dealing with multiple environments, we recommend the modular approach.

The modular approach

Starting with Spyder 3.3.1, you can install the modular spyder-kernels package into any Python environment (conda environment, virtualenv/venv, system Python, WinPython, etc) in which you wish to work, and then change the Python interpreter used by Spyder on its IPython consoles to point to the Python executable of that environment.

This takes a small amount of preparation and configuration, but is much "lighter" and quicker than a full Spyder installation into that environment, avoids dependency conflicts, and opens up new workflow possibilities.

To achieve this, follow these steps:

1- Activate the environment (e.g. myenv) in which you'd like to work (e.g. with conda activate myenv for conda, source myenv/bin/activate or workon myenv for virtualenv/venv, etc)

2- Install the spyder-kernels package there, with the command:

3- conda install spyder-kernels if using conda/Anaconda,

4- pip install spyder-kernels if using pip/virtualenv.

5- After installing via either method, run the following command inside the same environment:

python -c "import sys; print(sys.executable)"

and copy the path returned by that command (it should end in python, pythonw, python.exe or pythonw.exe, depending on your operating system).

6- Deactivate that environment, activate the one in which Spyder is installed (if you've installed it in its own environment) and start Spyder as you normally would.

7- After Spyder has started, navigate to Preferences > Python Interpreter > Use the following interpreter and paste the path from Step 3 into the text box.

8- Start a new IPython console. All packages installed in your myenv environment should be available there. If conda is used, the name of the current environment and its Python version should be displayed in Spyder's status bar, and hovering over it should display the path of the selected interpreter.

下面对我有用:

打开 Anaconda,设置新环境,然后 select 环境并点击播放图标,如下所示:

然后点击打开终端,在终端输入“spyder”,它会打开带有selected env的spyder。

注意:对我来说,直接从主页打开 Sypder 总是以基本环境打开。