149

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?

Community
  • 1
  • 1
tomaskazemekas
  • 5,038
  • 5
  • 30
  • 32
  • For future readers, [here](https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder#working-with-other-environments-and-python-installations) is the official guidance on how to do this. – mhdadk May 31 '22 at 20:12

11 Answers11

170

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

adelriosantiago
  • 7,762
  • 7
  • 38
  • 71
  • 6
    Yeah. Seems like one has to install a different instance of Spyder for each VE. – Mikhail Genkin Jul 31 '18 at 19:24
  • 4
    Can I do this from the command line? Running Navigator each time seems like a drag... – nocibambi Feb 09 '19 at 12:55
  • 1
    @nocibambi Yes, if you mean just to run Spyder in an env from cmd, see the bottom of the answer of tomaskazemekas. – questionto42 Aug 20 '20 at 08:53
  • After using this method, I notice that Anaconda installed Spyder _**in**_ the virtual environment. The size of the environment folder grew by ~700Mb. Doesn't this mean that I'm making my environment unnecessarily heavy? I want to use Spyder as my dev environment, but don't want its installation to be required when I deploy my script to a server. – Fabio Oliveira Apr 24 '21 at 12:01
  • @FabioOliveira The ~733Mb are mainly libraries and binaries that you may require in your projects. However if you decide not to include these libraries your final deploy will not require them. A script will very unlikely be more than 5Mb in space and a fully-working, deployed-script running on a prod-server with hundreds of libraries could require 300Mb. – adelriosantiago Apr 24 '21 at 18:28
  • this solution worked for me the first time. then i restarted spyder and have since been getting "an error occurred while starting the kernel". this error only occurs in the virtual environment – zunman Jun 14 '23 at 15:06
139

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

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

questionto42
  • 7,175
  • 4
  • 57
  • 90
tomaskazemekas
  • 5,038
  • 5
  • 30
  • 32
  • I see you are using linux. This did not work for me in windows 10. It did launch spyder, but with python 3.4 as before (I wanted 2.7). – mvbentes Oct 23 '15 at 00:08
  • 8
    I have tested this on Windows 7 and it worked. You don't need to add the word "source" at the beginning to activate the environment. To have Python 2,7, change the setting to python=2.7. – tomaskazemekas Nov 24 '15 at 12:29
  • 1
    Tried this in Windows 7 and it didn't work. Still using the normal python and numpy versions, not the ones specified in the environment – endolith Dec 27 '15 at 03:47
  • 4
    oh maybe you have to install spyder into that environment first? I created the environment with numpy instead of anaconda, so maybe it's running spyder from the main environment? – endolith Dec 27 '15 at 03:51
  • 4
    that does seem to be the issue. same thing with ipython but was fixed after installing it within the environment – endolith Dec 27 '15 at 03:57
  • 9
    I also found that on windows(10), just launching spyder from an anaconda environment still launched spyder with my default python, and I had to conda install spyder in the anaconda environment first to open spyder with that environment's python. – Max Power Sep 27 '16 at 19:34
  • windows users also note: in order to "activate myenv" correctly, your cmd or powershell should be in the anaconda directory, otherwise you may get a silent error. e.g. cd c:\anaconda2 activate myenv conda info -e – dreab Oct 03 '16 at 20:21
  • 9
    Also make sure you have spyder in that environment by `conda install -n myenv spyder` – shahar_m Jan 19 '17 at 14:05
  • ended up back here a year later. now I'm on linux mint (not windows) but still need to reinstall spyder in my anaconda environment before opening spyder from that env launches a spyder with the correct packages – Max Power May 29 '17 at 17:36
  • 1
    I was suffering from a similar issue with a conda python 3.5.2 environment. It turns out when I install spyder using `pip install spyder` it did not work. However when using `conda install spyder` it worked fine. – Willem Jun 23 '17 at 02:39
  • 1
    Seems like one has to install a different instance of Spyder for each VE: otherwise, Spyder from base environment will be opened – Mikhail Genkin Jul 31 '18 at 19:24
  • I use an Anaconda agnostic setup (pyenv + PyCharm + RStudio), and added Spyder via `pip install spyder`, I could not select an interpreter from an existing venv like we can with jupyter, pycharm and VS Code. Whenever I try to select the interpreter from the venv, Spyder keeps defaulting to the default python 3.8 interpreter where I installed Spyder. So I guess I have to `pip uninstall spyder` and install Spyder into my venv like you guys are saying. So every time I want to use Spyder with a different venv, I have to make sure it's installed in that venv??? Crazy. We could have 50 Spyders. – TokyoToo May 30 '20 at 07:54
  • Interesting. I just posted above this post so read that one too. I rebooted Spyder and got this message. `Your Python environment or installation doesn't have the spyder‑kernels module or the right version of it installed (>= 1.9.1 and < 1.10.0). Without this module is not possible for Spyder to create a console for you. You can install it by running in a system terminal: conda install spyder‑kernels or pip install spyder‑kernels`. So I guess in order to select an interpreter from a venv `spyder‑kernels` needs to be installed. First time I've seen that message on the net. – TokyoToo May 30 '20 at 07:57
  • I see. It just means Spyder needs to be installed into that venv. – TokyoToo May 30 '20 at 08:01
  • 1
    I pip uninstalled then reinstalled Spyder into my venv and it sees all the modules now so all is good. I can use Spyder without Anaconda which is nice. However I do not know why I cannot just have a single independent app instance of the Spyder IDE and select any interpreter from a venv like VS Code, Jupyter and PyCharm instead of having to install Spyder into every venv. So we might have 20 Spyders on our systems? Really insane. – TokyoToo May 30 '20 at 08:25
  • I added the accepted edit at the bottom. Afterwards, I realised that `conda activate myenv` is possible as `activate myenv` - as it is also written in the answer and comments - and my edit was refused three times. The last one because: "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer." Although I am "addressing" my own edit here. – questionto42 Aug 20 '20 at 09:00
  • 1
    @Lorenz It's not possible to see who made the last edit in the review queue, so it looks like you're editing the answerers text. In which your edit violates some guidelines. I've accepted now though – Ted Klein Bergman Aug 20 '20 at 13:12
  • This method worked but for my case I needed to add the `site-packages` in the virtual environment because I have a custom module and some pip installations that are not present in the global SYSPATH of python. – haddagart Nov 22 '22 at 21:57
70

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.

Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
Boussad
  • 701
  • 5
  • 2
  • 1
    After trying all the rest of this and elsewhere posts (two days trying!), this was the only working solution in order to set the proper environment envs inside Spider.. – Antonio Canepa Sep 03 '18 at 10:57
  • Don't require step (1), since step (2) already routed the interpreter to the environment. – 0xFK May 30 '19 at 05:24
  • 5
    @Feras For future readers: I also thought step 1 was redundant. But omitting it causes all sorts of crazy errors (and even causes the UI to bug out and Spyder to go unresponsive). – Faruk D. Nov 17 '19 at 12:37
  • You don't mean conda environments, do you? Otherwise you would fail to start Spyder from under the environment without installing Spyder there. If you installed it, you don't need to change python interpreter in the Spyder's preferences. – pch Nov 10 '21 at 15:16
59

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)

Tin Luu
  • 1,557
  • 16
  • 22
  • 2
    Spyder is usually installed as part of Anconda Python [package list](https://docs.continuum.io/anaconda/pkg-docs). Additional installation may be needed only in case of miniconda. – tomaskazemekas Mar 15 '16 at 09:20
  • 1
    Yes, that's right. In my case, I had my virtual environment installed before, and it's a light version, I only install very necessary packages. I guess there are someone being in my situation. – Tin Luu Apr 25 '16 at 03:18
  • @tomaskazemekas sir i installed miniconda3 :) how can i run my python code in spyder as i don't know how to run spyder? when i type "spyder" in miniconda prompt "could not recognize this command" error is generated. – Noman marwat Aug 04 '19 at 18:27
  • @Noman marwat Since spyder is not part of miniconda, you have to install it before running with command `conda install spyder` . – tomaskazemekas Aug 05 '19 at 09:51
  • After that, look for the symlink inside the `myenv/bin` directory and copy it in the `Applications` folder so you can easily find it and launch Spyder inside its environment without having to type in a small, black window! (you can easily add an icon to it too, if you're into icons) – PatrickT May 24 '20 at 08:00
56

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

Voila

Alexis
  • 1,343
  • 12
  • 15
  • Note that when using virtualenv & spyder 4.1.3 nothing appears at the bottom line confirming (everything worked at intended though) – Alexis Jun 03 '20 at 17:26
  • 3
    This is the correct answer! In addition, spyder installation can be in any conda virtual environment. It needs not to be in a base environment. – Sanchit Aug 04 '20 at 13:28
  • 3
    You are right @Sanchit you can "pip/conda" install spyder in each environmement then can call it from there but when you want just one install this is the solution – Alexis Aug 04 '20 at 15:49
  • When I used this method, installing spyder in the base environment and then setting interpreter to my virtualenv, the base environment had precedent in sys.path and I was importing packages from this environment instead of my virtualenv when there were duplicates. I ended up installing spyder into virtualenv. – jtorca Dec 04 '20 at 03:30
  • An alternate method to finding the python path by installing spyder-kernels+ using sys.path, one can also activate the specific conda environment in a terminal and type ```which python```. – Shalini Maiti Mar 24 '21 at 13:11
  • That's perfect! When in doubt, always go with the official reference. As a side note, you can also use Anaconda Navigator to create the virtual environment and install the spyder-kernels package in it. – Fabio Oliveira Apr 24 '21 at 13:20
  • Thank you! I use miniconda without the GUI and don't want to waste space installing spyder in every single environment. – ambitiousdonut May 27 '21 at 01:20
  • One should keep the version of spyder-kernels close both in base and new environment. These two failed to work: (base env) Spyder-3, spyder-kernels=0.5.2; (new env) spyder-kernels=2.1.3. Downgrading the version in new env helped. – pch Nov 10 '21 at 15:33
  • Thank you. This is the solution. It took me two full days to realize this is the solution. Initially Spyder5.1.5 worked in the anaconda base env. Two days ago, it became non-responsive in debug mode (by clicking debug_file button), but ok in the run mode. Even if the virtual environment has nothing but spyder itself, it is non-responsive. I reinstalled anaconda and it is working in base env now, but cannot get it to work in any fresh virtual environment. – VictorL Feb 05 '22 at 08:22
  • Best solution! Worked perfectly fine for me with miniconda, no need to re-install spyder in every venv. – Gaspar Avit Ferrero Mar 14 '23 at 08:37
11

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.

11

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.

Steven Kalt
  • 1,116
  • 15
  • 25
5

From Spyder official page on Github:

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.

rvimieiro
  • 1,043
  • 1
  • 10
  • 17
4

On 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, 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; though in my case, this step was not needed.

  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:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Anaconda3 (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.

questionto42
  • 7,175
  • 4
  • 57
  • 90
smoothware
  • 898
  • 7
  • 19
2

For me below worked:

Open Anaconda, setup new environment, then select the env and click on play icon as shown below:

enter image description here

Then click on Open Terminal, and type "spyder" in terminal, it will open the spyder with selected env.

Note: For me directly opening Sypder from Home page was always opening with base env.

Anand
  • 2,239
  • 4
  • 32
  • 48
0

I follow one of the advice above and indeed it works. In summary while you download Anaconda on Ubuntu using the advice given above can help you to 'create' environments. The default when you download Spyder in my case is: (base) smith@ubuntu ~$. After you create the environment, i.e. fenics and activate it with $ conda activate fenics the prompt change to (fenics) smith@ubuntu ~$. Then you launch Spyder from this prompt, i.e $ spyder and your system open the Spyder IDE, and you can write fenics code on it. Remember every time you open a terminal your system open the default prompt. You have to activate your environment where your package is and the prompt change to it i.e. (fenics).

desertnaut
  • 57,590
  • 26
  • 140
  • 166
batman47
  • 9
  • 3