How to run Spyder in virtual environment?

Posted on

Question :

How to run Spyder in virtual environment?

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.

Top two suggestions after research online are:

  1. to set up virtual environment first and to point change the preferences of Spyder , e.g here;
  2. 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?

Answer #1:

Here is a quick way to do it in 2021 using the Anaconda Navigator. This is the most reliable way to do it, unless you want to create environments programmatically which I don’t think is the case for most users:

  1. Open Anaconda Navigator.
  2. Click on Environments > Create and give a name to your environment. Be sure to change Python/R Kernel version if needed.

enter image description here

  1. Go “Home” and click on “Install” under the Spyder box.

enter image description here

  1. Click “Launch/Run”

There are still a few minor bugs when setting up your environment, most of them should be solved by restarting the Navigator.

If you find a bug, please help us posting it in the Anaconda Issues bug-tracker too! If you run into trouble creating the environment or if the environment was not correctly created you can double check what got installed: Clicking the “Environments” opens a management window showing installed packages. Search and select Spyder-related packages and then click on “Apply” to install them.

enter image description here

Answered By: adelriosantiago

Answer #2:

There is an option to create virtual environments in Anaconda with required Python version.

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):

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.)

And now to run Spyder with Python 3.4 just type:


Spyder with Python 3.4

EDIT from a reader:

For a normal opening, use “Anaconda Prompt” > activate myenv > spyder (then the “Anaconda Prompt” must stay open, you cannot use it for other commands, and a force-close will shut down Spyder). This is of course faster than the long load of “Anaconda Navigator” > switch environment > launch Spyder (@adelriosantiago’s answer).

Answered By: tomaskazemekas

Answer #3:

Additional to tomaskazemekas’s answer: you should install spyder in that virtual environment by:

conda install -n myenv spyder

(on Windows, for Linux or MacOS, you can search for similar commands)

Answered By: Tin Luu

Answer #4:

What worked for me :

  1. run spyder from the environment (after source activate)
  2. 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

Worked on ubuntu 16, spyder3, python3.6.

Answered By: Boussad

Answer #5:

To do without reinstalling spyder in all environments follow official reference here.

In summary (tested with conda):

  • Spyder should be installed in the base environment

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 (spyder3)

  • Finally in spyder Tool menu go to
    Preferences > Python Interpreter > Use the following interpreter and paste the environment python executable path

  • Restart the ipython console

PS: in spyder you should see at the bottom something like thisenter image description here


Answered By: Alexis

Answer #6:

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 answer which gave the following workaround:

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.

Answered By: Steven Kalt

Answer #7:

I just had the same problem trying to get Spyder to run in Virtual Environment.

The solution is simple:

Activate your virtual environment.

Then pip install Spyder and its dependencies (PyQt5) in your virtual environment.

Then launch Spyder3 from your virtual environment CLI.

It works fine for me now.

Answer #8:

On Windows:

You can create a shortcut executing

Anaconda3pythonw.exe Anaconda3envs<your_env> Anaconda3envs<your env>pythonw.exe Anaconda3envs<your_env>

However, if you started spyder from your venv inside Anaconda shell, it creates this shortcut for you automatically in the Windows menu. The steps:

  1. install spyder in your venv using the methods mentioned in the other answers here.

  2. (in anaconda:) activate testenv

  3. Look up the windows menu “recently added” or just search for “spyder” in the windows menu, find spyder (testenv) and

  • [add that to taskbar] and / or

  • [look up the file source location] and copy that to your desktop, e.g. from C:UsersUSERAppDataRoamingMicrosoftWindowsStart MenuProgramsAnaconda3 (64-bit), where the spyder links for any of my environments can be found.

Now you can directly start spyder from a shortcut without the need to open anaconda prompt.

Answered By: smoothware

Leave a Reply

Your email address will not be published.