Why is Spyder using libraries from base environment instead of virtual environment - python

I created and activated a virtual environment with virtualenv:
virtualenv venv
source venv/bin/activate
Then I installed spyder kernels and other libraries in the virtual environment:
pip install spyder-kernels==0.*
pip install numba==0.50
Then I launched Spyder in the virtual environment:
spyder
And I changed the python interpreter in Spyder to the correct path using: > Python > Preferences > Python interpreter > Use the following Python interpreter.
My Python interpreter in my virtual environment is
/Users/myname/venv/bin/python
I also restarted the Spyder console several times.
However, when I import libraries in Spyder, rather than the libraries installed in the virtual environment, Spyder uses the libraries from the base Anaconda environment.
import numba
print(numba.__version__)
0.38.0
print(numba.__file__)
/anaconda3/lib/python3.6/site-packages/numba/__init__.py
This is causing all kinds of errors in my code.
What can I do to force Spyder to only use libraries installed in my virtual environment?
P.S: My Spyder version is 3.2.8, and Python is 3.6.6.

I reinstalled the most recent version of Anaconda from here, which came with Spyder 4.0.1 and Python 3.7.6. That alone solved the problem for me.

Related

Why does not Spyder acknowledge the virtual environment?

I created a virtual environment in Conda for 2.7 named PY27 using the method of this thread:
How to run Spyder in virtual environment?
I have currently Python 3.7 installed and I wanted to emulate 2.7. However, although the environment looks as if it's working in the Anaconda Prompt switching correctly to the new PY27, in Spyder I still see 3.7.
In addition, I keep receiving this message:
"Note: you may need to restart the kernel to use updated packages."
I have tried to use the
conda init powershell command but I still receive the same message.
Thank you very much in advance for your help!
The python version in spyder's title bar is the python version of the environment in which spyder is installed. As of spyder 3.3, you can change the python kernel used by spyder without changing the python version of its environment.
First make sure you have spyder-kernels in your PY27 environment.
conda install -c conda-forge spyder-kernels
I'll recommend you install one of the spyder-kernels = 0* versions
Then execute python -c "import sys; print(sys.executable)" in your PY27 env prompt and copy the output. Open spyder and navigate to Tools -> Preferences -> Python interpreter and paste the command output there. You just told spyder to use yourPY27 env's python interpreter.
Reload spyder and you should see you're PY27 env's python kernel show up on the console.

Downloaded module with Anaconda, but it's not found in Pycharm environment

I successfully downloaded 'geohash' module using Anaconda. I confirmed it works by testing a line of code in Jupyter notebook. However, in Pycharm, it cannot find the geohash module.
I see the module 'geohash' clearly is in my Anaconda site-packages folder.
I then opened a new python file in Pycharm, and selected "New Conda Environment" as the Project interpreter. You can see the file's interpreter is Anaconda Project Default (Python 3.7) :
And yet, there is no 'geohash' module in the venv in Pycharm, and the module cannot be found:
I'm confused as to why this is so. I would assume all my modules installed with Anaconda would transfer over once the project interpreter is set to Anaconda Python 3.7. What am I doing wrong?
When creating a new environment with conda, only the default packages are installed at the beginning. When you created the Geohash environments, it was created with only the default packages conda comes with, and geohash is not a part of that.
You would need to install the geohash package to your particular environment using:
# Install pip in your environment
conda install -n GeoHash pip
# activate your environment
conda activate GeoHash
# Install the package
pip install Geohash
you can read more about managing environments here
I don't know which version of PyCharm you are using. I think you should choose to use existing environment and point to the environment (could be the conda default) where you installed the package. IIUC, choosing "New Environment" would create a new conda environment which does not have your package.

Python version in Programs and Features is different to that in Anaconda

I have Windows 10 Home 64-bit. In the Control Panel > Programs and Features menu, I can see that I have Python 3.5.2 (Anaconda 4.1.1 64-bit) installed. As I wanted to upgrade Python 3.5 to Python 3.6, I then ran this command in Anaconda Prompt conda install python=3.6.8. After the upgrading/installing process in Anaconda Prompt was finished, I ran this command python -V and it showed Python 3.6.8 :: Anaconda 4.1.1 (64-bit). However, when I refreshed the Control Panel > Programs and Features menu, it still showed Python 3.5.2 (Anaconda 4.1.1 64-bit). I then ran this command conda info --envs and it showed I only have the base environment.
Could anyone please advise:
Why are there two different information of Python versions in this case?
Which Python version is actually installed in my laptop?
In my Jupyter Lab Launcher, under Notebook section, I can see two Python symbols: Python 3 and Python [conda env:root]*. What are the differences between these two and which one should I use to launch a new notebook?
Thankyou!
Conda creates virtual environment inside your machine. The python version is different inside conda. If you want install different python inside conda you can decide the python at the time of creation of environment.
conda create -n TestEnv python=3.6.8
If you want update the python inside conda virtual environment, activate the virtual environment using
conda activate TestEnv
Now you can see the python version of your virtual environment
Deactivate your virtual environment and check python version using python -V this is the version of your Laptop Python or base version of python.

Conda: New python 3.7 environment created, but Spyder opened a python 3.5 environment instead

conda
conda create --name py37 python=3.7
activate py37
spyder
Those were the commands I had entered. But Spyder opened a 3.5.4 python instead which was the base environment's python version.
Some context:
I was trying to install selenium earlier but I could not import the module within python. So I installed a previous conda revision, and tried installing selenium again. Still, selenium could not be imported. Then I realized Spyder wasn't opening right. I believe it was working fine before this. When it changed, the Spyder IDE layout when I opened Spyder was changed.
What could be the problem? Do I need to reinstall conda?

Opening Spyder from a Python 2 Environment still runs Python 3 in Spyder

I have created a new environment, specifying the Python version as 2.7. This has worked correctly as when I activate the environment and I run on the command line:
python --version
It returns:
Python 2.7.15
However, when I then open Spyder from the command line in the same environment, Spyder is still using Python 3. I was expecting the Python version in Spyder to match the Python version in the environment. So what could be stopping it working as I expected? I am on Linux Ubuntu on a virtual machine.
Maybe you are launching it from the wrong environment.
Assuming you're using Anaconda, you should activate the py2 environment and launch spyder from it, but being sure to have installed it in there before.
Here a simple sequence as example:
1. Create a new env py2 based
>conda create -n <NAME_OF_YOUR_ENV> python=2.7
2. activate the environment
>activate <NAME_OF_YOUR_ENV>
3. install Spyder
<NAME_OF_YOUR_ENV> >conda install spyder
4. launch spyder
<NAME_OF_YOUR_ENV> >spyder
It should be enough
Do you have several python installed on your system? I guess you have 2.7.15 installed by default with your system and you installed python 3 (via Anaconda?) without adding it in your path.
Are you in spyder's directory when you launch it (and launch spyder with ./spyder)?
It seems I missed a step to install the Spyder package within the environment.
conda install spyder
It works as expected now.

Categories