Switch between spyder for python 2 and 3 - python

From what I have learnt in the documentation it states that you can easily switch between 2 python environments by just creating a new variable using command prompt
"conda create -n python34 python=3.4 anaconda" if i already have python 2.7 installed.
An environment named python 34 is created and we can activate it using "activate python 34" But all this happens like executing the above commands happens in my windows command prompt. I want to switch between python versions in spyder IDE, How to do this?

Spyder is launched from the environment that you're using.
So if you want to use python 3 in Spyder then you activate python34 (or whatever you named the environment with Python 3) then run spyder.
If you want to use python 2 in Spyder then you deactivate the python3 environment (or activate an environment in which you installed Python 2) then run spyder.
I do not believe that you can change environments once Spyder is launched.
N.B. you may need to install Spyder in each environment, depending on your set up, by first activating the environment then using conda install spyder.

Just go to preferences in spyder & then go to Python interpreter-> Use the following python interpreter: here, from browse files option, give path for your python2.exe file & then apply. Now your python2 doesn't have spyder-kernels module required to open console in spyder so install it by writing command in cmd python2 -m pip install spyder-kernels. Here python2 -m is used coz I have two versions of python installed

Just go to the directory where you have installed Spyder(use cd in command prompt), for me, it looks like "C:\Users\Rohan\Anaconda2" and type spyder in cmd. it will run your Spyder IDE.

Related

MacOS python command not finding active Conda environment from VS Code terminal

I recently got a new M1 MacBook - first time ever using a Mac - and immediately downloaded Miniconda to get it set up for some Python work.
I created some virtual environments (e.g. conda create -n myenv python=3.8) but when active, the python command defaults to Mac's Python 2.7, preventing me from running scripts from the command line in VS Code. For example:
conda activate myenv
(myenv)% which python
/usr/bin/python
where shows me the default 2.7 installation and the correct virtual environment version, but I can't access it.
(myenv)% where python
/usr/bin/python
/Users/user/miniconda3/envs/myenv/bin/python
Any idea how to get VS Code to find the proper Python version? It seems to work from the built in terminal, just not VS Code.
OS: Monterey 12.2.1
VS Code: 1.65.0
Miniconda: 4.10.1
Could you try to take advantage of python3 instead of python in the MacOS?
Or you can try to rename the python.exe to something others, such as python2.7 under the /usr/bin/python?
So the problem is, that in windows, you can change the path, but in mac there is this thing, that if you don't select the version, for example:
sudo python test.py
It will run Python 2.7, because it is installed, and it runs the lowest installed version. So try using
sudo python3 test.py
or specify the version in the terminal
sudo python3.8 test.py

terminal doesn't use correct python version in virtual environment

I have created a miniconda virtual environment
conda create --name finalenv python=3.6
Now when I activate it:
conda activate finalenv
and check:
python --version
It says:
2.7.16
Even though I created a virtual environment with python 3.6 version. This is in my Visual Studio Code Terminal. How can I fix this?
If I check python --versionoutside the virtual env, it's 3.8.10.
For example, if I try to install a particular library outside the virtual env, it works fine but throws an error if I run the same command inside the virtualenv since over there, the terminal is using an old python version #Samuel
In VScode do the following:
File -> Preferences -> Settings
Search for "python.pythonPath"
Change path to where you usually run python from (where python 3.6.13 is located on your machine)

Python 2.7 Script in Virtual Environment

I am running a Python 2.7 script that has specific dependencies/libraries (contained in a virtual environment) using anaconda prompt. Is there a way to run the script using code in a python 3 .py file in a different environment? Something like a library that allows me to open anaconda prompt in a specific environment (to then run the python 2.7 script). I couldn't seem to find it online. Any pointers would be appreciated.
Well you can select the environment from the drop-down box in the Anaconda Navigator home page if you have your virtual environment in Anaconda itself.
Or you can use
conda activate env-name
If you have your base conda in your terminal as default or else activate using
source ~/.bash_profile
then run
conda activate env-name
Hope this helps.

How to install Spyder for Python 2 and Python 3 and get Python 3 in my Spyder env?

I have Python 2.7 installed (as default in Windows 7 64bit) and also have Python 3 installed in an environment (called Python3).
I would like to use Spyder as my IDE. I have installed Spyder3 in my Python3 environment, but when I open Spyder 3 (from within my Python 3 env), then it opens Spyder for python 2.7 and not python 3.5 as I would've hoped for.). I don't know why.
I have done TOOLS--Preferences--Python Interpreter -- Use the following Python interpreter: C:\Users\16082834\AppData\Local\Continuum\Anaconda2\envs\Python3\python.exe, but this didn't work either.
Many of us are running multiple python environments; I am sure some of you might have managed to use Spyder for these different environments.
Please tell me how I can get Python 3 using this method.
One possible way is to run activate Python3 and then run pip install Spyder.
So, when you create a new environment with: conda create --name python36 python=3.6 anaconda
This will create an env. called python36 and the package to be installed is anaconda (which basically contains everything you'll need for python).
Be sure that your new env. actually is running the ecorrect python version by doing the following:
activate python environmentwith: active python36
then type: python
this will indicate what python version is running in your env. It turns out, for some reason, my environment was running python2.7 and not 3.6
The cool thing is that anaconda distribution comes with spyder. Just be sure that you run Spyder from within your environment.
So to do this: activate python36
then type: spyder
It will automatically open spyder3 for python3.
My initial issue was therefore that even though i created a python3 environment, it was still running python2.7. But after removing the old python3 environment and creating a new python3 env. and installing the desired libraries/packages it now works perfect.
I have a 2.7 and 3.6 environment which can both be edited with spyder2 and spyder3 IDE

How to install 2 Anacondas (Python 2 and 3) on Mac OS

I'm relatively new in macOS. I've just installed XCode (for c++ compiler) and Anaconda with the latest Python 3 (for myself). Now I'm wondering how to install properly second Anaconda (for work) with Python 2?
I need both versions to work with iPython and Spyder IDE. Ideal way is to have totally separate Python environments. For example, I wish I could write like conda install scikit-learn for Python 3 environment and something like conda2 install scikit-learn for Python 2.
There is no need to install Anaconda again. Conda, the package manager for Anaconda, fully supports separated environments. The easiest way to create an environment for Python 2.7 is to do
conda create -n python2 python=2.7 anaconda
This will create an environment named python2 that contains the Python 2.7 version of Anaconda. You can activate this environment with
source activate python2
This will put that environment (typically ~/anaconda/envs/python2) in front in your PATH, so that when you type python at the terminal it will load the Python from that environment.
If you don't want all of Anaconda, you can replace anaconda in the command above with whatever packages you want. You can use conda to install packages in that environment later, either by using the -n python2 flag to conda, or by activating the environment.
Edit!: Please be sure that you should have both Python installed on your computer.
Maybe my answer is late for you but I can help someone who has the same problem!
You don't have to download both Anaconda.
If you are using Spyder and Jupyter in Anaconda environmen and,
If you have already Anaconda 2 type in Terminal:
python3 -m pip install ipykernel
python3 -m ipykernel install --user
If you have already Anaconda 3 then type in terminal:
python2 -m pip install ipykernel
python2 -m ipykernel install --user
Then before use Spyder you can choose Python environment like below!
Sometimes only you can see root and your new Python environment, so root is your first anaconda environment!
Also this is Jupyter. You can choose python version like this!
I hope it will help.
This may be helpful if you have more than one python versions installed and dont know how to tell your ide's to use a specific version.
Install anaconda. Latest version can be found here
Open the navigator by typing anaconda-navigator in terminal
Open environments. Click on create and then choose your python version in that.
Now new environment will be created for your python version and you can install the IDE's(which are listed there) just by clicking install in that.
Launch the IDE in your environment so that that IDE will use the specified version for that environment.
Hope it helps!!

Categories

Resources