Cannot import pandas when using Spyder under virtual environment - python

In my "base" environment, installing pandas and then importing it using the Spyder IDE works just fine.
However, this is no longer the case when working under a virtual environment.
In my terminal, I created a Python 2.7 environment named bodhi:
conda create -n bodhi python=2.7
I activated it:conda activate bodhi
I then installed spyder: sudo -i apt-get install spyder
And finally I installed pandas: conda install pandas
Typing conda list confirms that pandas 0.24.2 was installed properly.
After opening Sypder, typing import pandas in the IPython console returns the following ImportError: No module named pandas.
What am I doing wrong?
I am fairly new to virtual environments so I apologize if the question seems obvious!

Your conda env isn't the same as your kernel. Open an IPython console Ctrl-T and do your installs and environment management there as described here.
You can also work with your kernel from the console using some of the commands described here:
https://stackoverflow.com/a/28840041/10553976
Importantly: path/to/python -m ipykernel install <options>

Related

i install conda installed and Python installed seperately

i have Conda installed and Python installed separately, when i run .py file it is referencing to python's packages not from Conda so some pkg are missing(pandas), Conda has pandas but not Python. all default installations are go to Conda pkg files. how do i install pandas to Python that is not in Conda?
To run a file from conda, simply go to your anaconda prompt and cd into the drive. Similarly, you can search for your python prompt from the start bar and run code from there.
To install pandas into your python installation type:
pip install pandas
If you are running code from an IDE or code editor, you need to go into your settings and change the default runtime environment to anaconda. Even easier, open your anaconda navigator and then run your editor from there.
Your conda installation can't not have python, although it might be running the wrong version of python for your code. To change the version of python type:
conda install python=$pythonversion$
into your conda prompt.

ModuleNotFoundError: 'sklearn' in Jupyter notebook

Using Conda (4.8) on pyhthon 3.7, on Win10. I have scikit learn installed using conda conda install scikit-learn . Tried a few things: also installed it in the env conda install -n my_env scikit-learn. Also tried installing conda install -c anaconda ipython - nothing has worked.
I can list it:
scikit-learn 0.22 py37h6288b17_0
But in juypter notebook get error
from sklearn.datasets import fetch_lfw_pairs (tried couple other commands too)
ModuleNotFoundError: No module named 'sklearn'
But If I use Anaconda UI Navigator to launch notebook everything works fine
Update
I tried this command line option did not work for me, despite plenty effort and help & support from the community (as below). Meanwhile the Jupyter notebook can also be launched from the Anaconda UI itself. That always was working for me - no configuration or setup needed (none). I have not found any limitations etc with this yet (and you do get the exact same notebook). For advanced/unique use cases where you may need to fine tune your configuration cmd line could be helpful, I am not there.
Likely, you are loading the wrong kernel when you start your notebook.
Here is a barebones way to set up the environment:
conda create -n testenv python=3.7 -y
conda activate testenv
conda install scikit-learn
conda install ipython
conda install notebook
python -m ipykernel install --user --name testenv
When you click on new in the browser you will have an additional option next to python3, namely the kernel you just registered. I just tested this with anaconda 4.7 and I could import sklearn.
edit:
The code in the answer creates a new python environment. Then, it installs ipython and jupyter notebook in that environment and makes sure that this environment can be used with jupyter notebook (i.e. registering the ipykernel).
Now of course besides scikit learn, no other libraries have been installed within that specific environment.
So, if you want to use more libraries, you have to go to the command line, activate the environment, and install the libraries you want to use:
conda activate testenv
conda install scipy numpy matplotlib
To then run jupyter notebook from the environment, after you have installed all the things you want (and after having closed the command prompt or having deactivated the environment), you can do
conda activate testenv
jupyter notebook
in the command prompt.
Jupyterlab would usually use the environment inside which you launch it. For example:
If you activate my_env first and then do jupyter lab from terminal, it should detect the environment.
Incase it fails, go to Kernel -> Change Kernel and select the kernel you want to use.
Note: While creating a new kernel, I always use the display-name parameter which helps. You can do something like:
python -m ipykernel install --user --name my_env --display-name "Python (my_env)"
Hope this helps.
To fix this problem, you need to manually install this package in Anaconda.
How do I install? Open your Anaconda Prompt and run the below command:
conda install -c conda-forge scikit-learn
Then restart Jupyter Notebook and import this package.
I think the problem is that the environment is not activated. Try conda activate my_env first, and then type jupyter notebook.
The first thing you can do is:
import sys
print(sys.path)
Check if /path/to/anaconda/envs/my_env/lib/python3.7/site-packages exists in the path.
I find it useful to print the current sys.path so that I know where it is looking at.
conda info --envs
when testEnv exists:
conda activate testEnv
conda list scikit-learn

Jupyter python3 notebook cannot recognize pandas

I am using the Jupyter notebook with Python 3 selected. On the first line of a cell I am entering:
import pandas as pd
The error I get from the notebook is, ImportError: No module named 'pandas'. How can I install pandas to the jupyter notebook? The computer I launched the Jupyter notebook from definitely has pandas.
I tried doing:
!pip install pandas
And it says it is already installed but for Python 2.7 at the bottom. My script shows it is a Python 3 script at the top though.
When I do echo $PATH in Ubuntu is shows that '/home/user/anaconda2/bin' is on the first entry. I think I may need to change this to be anaconda3?
UPDATE: When I try and launch a Python3 script through jupyter the command line which launched Jupyter gives me the error "ImportError: No module named 'IPython.paths'. Then there is a timeout waiting for 'kernel_info' reply. Additionally, I tried removing anaconda but still experience the same error. I have tried to make so many quick fixes now, that I am not sure what the next step is to get this working.
As your default python version is 2.x , if you don't have any emphasis on the python 3.x you can try from the first by the below scripts.
pip install --upgrade pip
pip install jupyter
then in jupyter notebook:
!pip install pandas
The version of notebook will be 2.x.
Otherwise install pip3 by the below Linux commands.
sudo apt-get install python3-setuptools
sudo easy_install3 pip
now you can add pandas to the notebook by !pip3 install pandas.
This worked for me
in Jupiter notebook
import sys
print(sys.executable)
copy path eg:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
install the module on the terminal like this.
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pandas
Or it can be installed directly from Jupyter Cell as follows
! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pandas
If you use anaconda already as a distribution, stop using pip in that context. Use conda instead and you will stop having headaches. The command lines and procedures for setting up a new environment are pretty well documented here.
Basically upgrading python or having specific branches:
conda update python
conda install python=3.5
Or using specific environments:
conda create -n py35 python=3.5 anaconda
simple step to resolve the problem is
( NOTE: Remember what you have selected python 2 or python 3).
for python 2+
!pip install pandas
or if you have user permission error type
!pip install pandas --user
for python 3+
!pip3 install pandas
or if you have user permission error type
!pip3 install pandas --user
If pip3 is install you could run
!pip3 install pandas
This is what i have done in my system:
I have installed both anaconda for python 2.7 and anaconda for python 3.5. Anaconda helps keep both the environment separate.
In Ubuntu:
The directory structure is like this:
anaconda2/bin/
anaconda3/bin/
Whenever i want to use python 2.7 i go to anaconda2/bin/ and create an environment or activate already existing environment and install or import all the necessary packages and same goes for python3.5 (go to anconda3/bin/ create or activate the required environment). This helps me keep things separate.
Since you are using anaconda you should first use "conda install " if that package is not found, then you can use pip install .
In Windows:
If you install both anaconda2 and anaconda3, its quite easy..
the shortcuts for anaconda prompt are in C:\Users\your-username\
there will be two folders anconda2 and anaconda3, you can start conda prompt for python2.7 from anaconda2 and python3.5 from anconda3
So, once you start the anaconda prompt you can just type "jupyter notebook" to open jupyter notebook in browser and import pandas(or any package).
You can check this link:
http://conda.pydata.org/docs/test-drive.html#managing-conda
I meet the same problem in jupyter notebook, and I run the command below and solve my problem:
!pip install pandas
For Windows
The first step is to create a new conda environment. A conda environment is like a virtualenv that allows you to specify a specific version of Python and set of libraries. Run the following commands from a terminal window:
conda create -n name_of_my_env python
This will create a minimal environment with only Python installed in it. To put your self inside this environment run:
source activate name_of_my_env
On Windows the command is:
activate name_of_my_env
The final step required is to install pandas. This can be done with the following command:
conda install pandas
To install a specific pandas version:
conda install pandas=0.20.3
To install other packages, IPython for example:
conda install ipython
To install the full Anaconda distribution:
conda install anaconda
If you need packages that are available to pip but not conda, then install pip, and then use pip to install those packages:
conda install pip
pip install django
Simple solution : In a Notebook's cell type and execute the code:
import sys
!{sys.executable} -m pip install pandas
Iuse window 7 for work and I had the same problems when I tried to import Pandas. So I tried to install packages under each environment:
Run cmd and type the following code:
activate py27
conda install pandas
If the system asks you do you want to install the following new packages, choose Y for [yes]
And install pandas for each different environment if you installed Python27, Python 35 and Python 36, as I did.
Then problem solved if you run jupyter notebook again and you can import pandas successfully.
You can also solve the same problem for packages like numpy, scipy, etc..
Maybe its a broken (pip) installation. Following worked for me:
sudo apt --fix-broken install
Followed by:
sudo pip3 install pandas
Hope this helps.
I was getting the error
modulenotfounderror: no module named 'pandas'
in jupyter.
I tried the command:
!pip install pandas
and it worked like a charm.
My pandas version was 0.20.3 I have updated to 0.25 using conda update pandas. I have checked in Command Line Interface(CLI) its pd.__version__ '0.25.1'.
In Jupyter notebook its showing '0.20.3'.
Please restart jupyter notebook. note is cache your pandas. or you can create a new notebook
One silly mistake that you could make is to invoke Jupyter with your local machine
python installation and not the anaconda python. I had the same problem and just
setting the PATH did not work for me.
FIX: By default, anaconda binaries are in "<Path_to_anaconda>/bin". For example, in my case, they are in "/home/klakhotia/anaconda3/bin". This directory will also contain the binary to anaconda jupyter. Launch the jupyter from here or create an alias in your .bashrc that points to this file.
What worked for me was:
pip3 install pandas jupyter

discrepancy in numbers of installed modules between ipython and ipython notebook

Hi I have a curious (and maybe trivial who knows?) problem.
For the requirements of a course on coursera I have created a python 2.7 environment in my anaconda distribution and installed DATOs' GraphLab Create modules.
While the simple "import graphlab" functions properly from the python command line or the ipython console it fails in a jupyter notebook.
checking installed modules with pip.get_installed_distributions() yields 148 modules in ipython console and only 42 in the jupyter notebook!
So, what is at work here? :-)
Thanks for any hint,
Alain
As you said you have created an environment with Python 2.7, you thus need to register this environment with the notebook.
Activate the environement:
$ source activate myenv
Then install the kernelspec for this environment:
(myenv)$ python -m ipykernel install --name myenv
You will need to install ipykernel in this env if it is not installed.
Now when starting a notebook, you can create a new notebook using this env, and/or change the currently used env, using the Kernel menu.

IPython Notebook - python3 not found after uninstalling Anaconda3 for Anaconda

I uninstalled Anaconda 2.3 with Python 3.4.3 and then installed the same Anaconda version with Python 2.7.10.
When I open a notebook via $ ipython notebook "Example Notebook.ipynb" it tries to use the python3 kernel as opposed to opening with the installed python2. Of course I get the error python3 kernel not found.
How can I get ipython notebooks to open with the python2 kernel? I've tried to uninstall ipython and ipython notebook, then delete .ipython and .jupyter from my user directory in case there were any defaults set in these folders, then reinstalled both. Still get the same problem.
Any help would be appreciated
You can install several python versions alongside each other. Just create another environment (replace "all my packages" with the names of the packages).
conda create --name mypy_27 python = 2.7
or
conda create --name mypy_34 python = 3.4
afterwards you can activate the environments by typing
source activate mypy_34
if you then do
conda install "all your packages"
you install the desired packages in the active environment.
You can do much more.

Categories