How to run Spyder in virtual environment?
Solution 1
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
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).
Solution 2
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:
- Open Anaconda Navigator.
- Click on Environments > Create and give a name to your environment. Be sure to change Python/R Kernel version if needed.
- Go "Home" and click on "Install" under the Spyder box.
- 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.
Solution 3
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
Worked on ubuntu 16, spyder3, python3.6.
Solution 4
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)
Solution 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 this
Voila
tomaskazemekas
Data Software Developer coding in Python and Scala. Areas of interest are blockchain applications, machine learning, quantitative finance, data parsing, natural language parsing and open data. Open for consulting projects. You can contact me on tomaskazemekas(at)gmail.com LinkedIn profile Hacker News: tomaskazemekas
Updated on July 08, 2022Comments
-
tomaskazemekas almost 2 years
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:
- 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?
-
mvbentes over 8 yearsI 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).
-
tomaskazemekas over 8 yearsI 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.
-
endolith over 8 yearsTried 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 over 8 yearsoh 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 over 8 yearsthat does seem to be the issue. same thing with ipython but was fixed after installing it within the environment
-
tomaskazemekas about 8 yearsSpyder is usually installed as part of Anconda Python package list. Additional installation may be needed only in case of miniconda.
-
Tin Luu about 8 yearsYes, 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.
-
Max Power over 7 yearsI 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.
-
dreab over 7 yearswindows 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
-
shahar_m over 7 yearsAlso make sure you have spyder in that environment by
conda install -n myenv spyder
-
Max Power almost 7 yearsended 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
-
Willem almost 7 yearsI 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 usingconda install spyder
it worked fine. -
masad almost 6 yearsThis doesnt work with python2 under ubuntu 16 anymore. Any ideas of a work around?
-
Mikhail Genkin almost 6 yearsSeems like one has to install a different instance of Spyder for each VE: otherwise, Spyder from base environment will be opened
-
Mikhail Genkin almost 6 yearsYeah. Seems like one has to install a different instance of Spyder for each VE.
-
Antonio Canepa over 5 yearsAfter 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..
-
Jerusalem Programmer over 5 yearsPython 2 is deprecated. It is recommended and advisable to upgrade yourself to using Python 3.
-
nocibambi about 5 yearsCan I do this from the command line? Running Navigator each time seems like a drag...
-
Feras almost 5 yearsDon't require step (1), since step (2) already routed the interpreter to the environment.
-
Noman marwat almost 5 years@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.
-
tomaskazemekas almost 5 years@Noman marwat Since spyder is not part of miniconda, you have to install it before running with command
conda install spyder
. -
cheekybanana over 4 years@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).
-
PatrickT almost 4 yearsAfter that, look for the symlink inside the
myenv/bin
directory and copy it in theApplications
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) -
TokyoToo almost 4 yearsI 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 topip 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 almost 4 yearsInteresting. 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 venvspyder‑kernels
needs to be installed. First time I've seen that message on the net. -
TokyoToo almost 4 yearsI see. It just means Spyder needs to be installed into that venv.
-
TokyoToo almost 4 yearsI 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.
-
Alexis almost 4 yearsNote that when using virtualenv & spyder 4.1.3 nothing appears at the bottom line confirming (everything worked at intended though)
-
Sanchit almost 4 yearsThis is the correct answer! In addition, spyder installation can be in any conda virtual environment. It needs not to be in a base environment.
-
Alexis almost 4 yearsYou 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
-
questionto42standswithUkraine over 3 years@nocibambi Yes, if you mean just to run Spyder in an env from cmd, see the bottom of the answer of tomaskazemekas.
-
questionto42standswithUkraine over 3 yearsI added the accepted edit at the bottom. Afterwards, I realised that
conda activate myenv
is possible asactivate 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. -
Ted Klein Bergman over 3 years@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
-
jtorca over 3 yearsWhen 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.
-
Shalini Maiti about 3 yearsAn 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
. -
Fabio Oliveira about 3 yearsAfter 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 about 3 yearsThat'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.
-
adelriosantiago about 3 years@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.
-
ambitiousdonut almost 3 yearsThank you! I use miniconda without the GUI and don't want to waste space installing spyder in every single environment.
-
pch over 2 yearsYou 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 over 2 yearsOne 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.
-
VictorL over 2 yearsThank 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.