Can't install python packages for a conda environment - python

I could use some clarification regarding anaconda envs and the installed packages. I have just began using environments other than the base to keep my installations clean.
I just deleted my anaconda3 folder, installed it from scratch, made a new environment and tried to run a jupyter-notebook.
When reading excel via pandas I get the error Missing optional dependency 'xlrd'
prompting me to install xlrd via conda or pip.
So I open the terminal, activate the env I’m working in and install it using conda. I close the JN from the terminal, reopen it and run the same code. The problem persists.
I repeat the procedure, this time indicating the name of the env at the end. Problem persists.
I checked which python in the terminal, it is indeed the anaconda3/envs/newenv/bin/python
I do the same (in the new env) using pip. I use the command .../anaconda3/envs/newenv/bin/python -m pip install xlrd. I get the message Requirement already satisfied: xlrd in ./anaconda3/envs/newenv/lib/python3.7/site-packages (1.2.0).
I even tried installing the package inside the notebook using !conda install xlrd, still I get the same error.
Finally, I open Jupyter via the base env, and the package works there perfectly.
I have no idea why I can’t install this under the newenv, which was the point of having local envs after all.
Thanks heaps for your help!

You need to install the kernel in Jupyter to be able to use it.
jupyter kernelspec list
That command will give you the list of kernels you have. I am assuming it only shows you Python3
You will now need to install a kernel. Remember to do this while inside your virtual environment
python3 -m pip install ipykernel
python3 -m ipykernel install --user --name <your-new-kernel-name>
You should now see this in Jupyter notebook. Select the kernel in Jupyter and you should be good to go.
Another thing you may want to try is to install Jupyter while inside your virtual environment. While inside your virtual environment, you could do:
python3 -m pip install jupyter lab
and then while still inside your virtual environment, run jupyter after checking which jupyter. It should solve your problem as well.

Related

Unable to install jupyter notebook

I am receiving the following download error when I attempt to install Jupyter Notebook on Windows:
ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'C:\\Users\\*redacted*\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python38\\site-packages\\jedi\\third_party\\django-stubs\\django-stubs\\contrib\\contenttypes\\management\\commands\\remove_stale_contenttypes.pyi'
I located the commands folder and the file remove_stale_contenttypes.pyi was not present. I did a file search for my CPU and the file was not found in another location.
I have never used python, pip, or jupyter before. I am attempting to install them in preparation for a class.
Make sure the maximum file path length limitation is turned off on your windows machine.
In the Registry Editor, use the left sidebar to navigate to the following key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
On the right, find a value named LongPathsEnabled and double-click it. If you don’t see the value listed, you’ll need to create it by right-clicking the FileSystem key, choosing New > DWORD (32-bit) Value, and then naming the new value LongPathsEnabled.
In the value’s properties window, change the value from 0 to 1 in the “Value data” box, and then click OK.
Here is a link to an article describing how to do this: https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/
Try uninstalling virtualenv or pipenv (whichever you are using) and then reinstalling. If this doesn't work try installing conda. There are two versions of it:
Anaconda
Miniconda
I would recommend going with miniconda as it is a lightweight installation but does not have a GUI. Here is the link to install it. Afterwards to create a virtual environment do this:
Go to the conda terminal or cmd and type in conda create --name myenv(and change the name of the env to whatever you like). This should create your environment.
Afterwards to activate it, type in conda activate name (Name is again what you put up there) Thats it. You have now created a conda env. So afterwards, whenever you want to access this enviornment again, use the activate command.
As for installing jupyter notebook, first activate your env and the run this:
conda install -c conda-forge notebook
This should install jupyter notebook in that environment. To access that jupyter notebook again, always activate the enviornment and then type in jupyter notebook. If this seems a bit too much for you, you should actually have a program named jupyter notebook(env name) in your computer after you successfully installed jupyter. Just click on that and it will handle everything for you.
Please let me know if you have trouble doing this.
The easiest way to setup Jupyter Notebook is using pip if you do not require conda. Since you are new to python, first create a new virtual environment using virtualenv.
Installing pip (Ignore if already installed):
Download get-pip.py for Windows and run python get-pip.py
Installing virtualenv: pip install virtualenv
Creating a new virtual environment: virtualenv your_env_name
Activate Virtualenv: your_env_name\Scripts\activate
Installing Jupyter Notebooks: pip install notebook
You can launch the notebook server using: jupyter notebook

ModuleNotFoundError in Jupyter with pipenv

I am at a Python boot camp this weekend but I have not been able to even use Python on my computer because of this issue. All my instructors are stumped too.
The issue is that I get the ModuleNotFoundError on Jupyter with multiple different packages, including Pandas and Requests (but oddly enough, BeautifulSoup and CSV work fine.)
Here is how I start a new Jupyter file:
Create a new directory
Install jupyter and pandas with this command: pipenv install jupyter pandas
Activate virtual environment: pipenv shell
Launch Jupyter: jupyter notebook
Create new Python 3 notebook
At this point, I try a command like import pandas as pd and get back the ModuleNotFoundError.
I am using Python version 3.6.5.
Attempts to fix this that have failed:
double-checked that pandas is installed in my virtual environment with pip graph
created completely new directory
pipenv install jupyter pandas --skip-lock
Uninstalled everything system-wide with these commands:
pip freeze > requirements.txt
pip uninstall -r requirements.txt -y
Updated pandas
Used virtualenv instead of pipenv
virtualenv first-python-notebook
cd first-python-notebook
cd Scripts
activate
cd ..
pip install jupyter pandas
I tested that pandas could be imported when I used python in the command shell (yes) -- still didn't work on Jupyter.
My instructor thinks the issue is that system-wide packages are interfering with virtual ones but we have been working for hours and cannot figure out how to fix this.
Any help would be greatly appreciated. Please include detailed instructions as I am a beginner.
If you're getting 'ModuleNotFoundError: No module named xxyyzz' in jupyter, but the module can be imported by running python via the pipenv shell (pipenv run python -c "import xxyyzz; print(xxyyzz.__version__)":
it's probably jupyter's python path isn't set properly in the kernel config file: ..\jupyter\kernels\<myProjectName>\kernel.json
the kernel needs to be created within the pipenv shell to pick up the right path
With a fresh pipenv install:
pip install pipenv
cd <project directory>
export PIPENV_VENV_IN_PROJECT=1 # creates .venv in project directory
pipenv --python=/path/to/python --site-packages # use python executable for your system or environment
pipenv shell # work in project's virtual environment
python -m ipykernel install --user --name=<myProjectName> # create jupyter kernel for project
exit # exit project's virtual environment
pipenv run jupyter notebook # start jupyter from project directory
in jupyter, choose the kernel "myProjectName"
this post provides additional explanations
Why don't you try to install ipykernel with Anaconda virtual env?
It'll will be more easy to handle.
If you haven't previously used Anaconda before, just go to the official website
https://www.anaconda.com/download/ and download the newest version for your OS.
Then, follow these steps.
Execute Anaconda prompt.
Type 'conda create -y -n $ENVIRONMENT_NAME ipykernel'
Type 'conda activate $ENVIRONMENT_NAME'
Type 'conda install -y $PACKAGES_TO_BE_INSTALLED'
Type 'python -m ipykernel install --user --name $NAME --display-name $IPYKERNEL_NAME'
This ipykernel name will be presented on your list of kernels in jupyter notebook.
You can findout the list of kernels installed by typing jupyter kernelspec list.
Hope this helps!
Thanks for the advice. However, I was advised specifically not to install Anaconda -- can't quite remember the reason but I think it's because, basically, if I ever decided I wanted to use something else then it would be a real headache to switch. I'm happy to hear your reasoning if you disagree with that.
I ended up solving the issue by uninstalling every package both within the virtual environment and the larger computer system, then re-installing it in both places. It worked, but I'm sort of confused as to what the point of a virtual environment is, if I still had to install everything twice.

Jupyter can't find keras' module

I have installed Tensorflow and Keras by Anaconda (on Windows 10), I have created an environment where I am using Python 3.5.2 (the original one in Anaconda was Python 3.6).
When I try to execute import keras as ks, I get ModuleNotFoundError: No module named 'keras'.
I have tried to solve this issue by sys.path.append(C:\\Users\\ ... \\Anaconda3\\python.exe)
with both notebook and console, but I continue to get the same error.
How could I solve this issue?
Please try the following:
Run these in the jupyter notebook cell:
import sys
sys.path
sys.executable
It may not be pointing to your virtual environment but to the root
The fix is to install the jupyter notebook from inside your virtual environment
$ . your_env/bin/activate
(your_env)$ python -m pip install jupyter
Now you can import tensorflow or keras
Jupyter uses iPython under the hood, for python. So when you install Jupyter, it will also install iPython. There was one issue when I installed keras and Jupyter: I already have iPython installed in my root Anaconda environment. This is the output after I install Jupyter and keras:
In [2]: import sys; sys.path
Out[2]:
['/home/user/anaconda3/bin',
'/home/user/anaconda3/lib/python36.zip',
'/home/user/anaconda3/lib/python3.6',
'/home/user/.ipython']
Notice that even though I am inside my conda environment, it still looks for libraries in my root conda environment. And of course keras isn't there.
The step to fix is simply re-activate my environment, with:
source deactivate && source activate [my_env]
Then I am using a correct ipython:
Out[2]:
['/home/user/anaconda3/envs/ml3/bin',
'/home/user/anaconda3/envs/ml3/lib/python36.zip',
'/home/user/anaconda3/envs/ml3/lib/python3.6',
'/home/user/.ipython']
(Not an answer but some troubleshooting hints)
sys.path is not the path to your Python executable, but the path to the libraries.
Check where Keras is installed and check your sys.path. How exactly did you install Keras?
Try to execute the same command from the Python interpreter. Do you have the same issue?
How did you install Jupiter, is the sys.path visible from there the same as sys.path visible from your Python interpreter?
Do Jupiter and Keras use the same version of Python?
You might try to uninstall Jupiter and install it again, and hope that the new installation picks up the packages which are already installed. What could happen is that you have more than one Python installation and different libraries being installed to the different places. sys.path, when requested from different environments, might give you a hint if that's true.
The kernel in console and jupyter are not necessarily the same, and the problem might be that you are not on python 3.5.
python --version
should tell you what is running in the console, and in jupyter you should see it as a choice on starting a new notebook. For me, the information in
Using both Python 2.x and Python 3.x in IPython Notebook
was very helpful.
I have realized that I had two different Jupyter's directories, so I have manually deleted one on them. Finally, I have reinstalled Anaconda. Now Keras works properly.
If you are a windows/mac user who are working on Jupyter notebook “pip install keras” doesn't help you .Try the below steps.It was solved for me
1. In command prompt navigate to the “site packages” directory of your anaconda installed.
2. Now use “conda install tensorflow” and after “conda install keras”
3. Re-start your Jupyter notebook and run the packages.
Acually, I did this command pip install keras and sudo -H pip3 install keras and pip3 install keras. None of them worked. I added the following command and everything worked like a charm:
pip install Keras. Yes a capital 'K'
Here's how I solved this problem.
First, the diagnosis. When I run which python in a terminal window on my Mac (the same terminal I used to launch jupyter, I get /Users/myusername/.conda/envs/myenvname/bin/python, but when I run the same command from a terminal within Jupyter, I get /usr/bin/python. So Jupyter isn't using the correct python executable; the version it's using doesn't have any of my packages installed.
But which jupyter returns /usr/bin/jupyter; it's using a version of jupyter that isn't coming from within my conda environment. I ran conda install jupyter and now which jupyter returns /Users/myusername/.conda/envs/myenvname/bin/jupyter (for some reason I had to restart the terminal window for this to take effect.) Then if I relaunch jupyter notebook, the notebook is using the correct version of Python and I have access to all my installed conda packages. 👍
I had a similar problem.
I added the Conda environment as a new kernel.
First, install ipykernel:
conda install ipykernel
Next, create the kernet:
python -m ipykernel install --user --name tf-gpu --display-name "TensorFlow-GPU"
Now, when you run your notebook, change the kernel to the new one, to "TensorFlow-GPU" in this example.

Running Jupyter notebook in a virtualenv: installed sklearn module not available

I have installed a created a virtualenv machinelearn and installed a few python modules (pandas, scipy and sklearn) in that environment.
When I run jupyter notebook, I can import pandas and scipy in my notebooks - however, when I try to import sklearn, I get the following error message:
import sklearn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-8fd979e02004> in <module>()
----> 1 import sklearn
ImportError: No module named 'sklearn'
I am able to import all modules, at the command line - so I know they have been successfully installed:
(machinelearn) me#yourbox:~/path/to/machinelearn$ python -c "import pandas, scipy, sklearn"
(machinelearn) me#yourbox:~/path/to/machinelearn$
How can I import sklearn in my jupyter notebook running in a virtualenv?
You probably have not installed jupyter / IPython in your virtualenv. Try the following:
python -c "import IPython"
and check that the jupyter command found in your $PATH is the one from the bin folder of your venv:
which jupyter
For windows users in a powershell console, you can use the following to check that the jupyter command in your $env:Path is the one from the Scripts folder of you venv:
get-command jupyter
Edit: if this is the problem, just run python -m pip install jupyter in your venv.
Edit 2: actually you might also need:
python -m ipykernel install --user --name=my-virtualenv-name
and then switch the kernel named "my-virtualenv-name" in the jupyter user interface.
Edit 3: maybe the --user flag in the last command is a bad idea:
python -m ipykernel install --name=my-virtualenv-name
Another approach to take is to have one global jupyter installation, but to point to different kernels to run as the backend.
That approach is outlined here in their docs:
http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
Copying below in case the link breaks:
You can use a virtualenv for your IPython notebook. Follow the following steps:
Install the ipython kernel module into your virtualenv
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install ipykernel
Now run the kernel "self-install" script:
python -m ipykernel install --user --name=my-virtualenv-name
Replacing the --name parameter as appropriate.
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:
create a virtual environment
mkvirtualenv --no-site-packages --python=/your/python/path your_env_name
Activate the virtual environment
workon your_env_name
Install Jupyter and other packages
pip install jupyter, numpy
Add a new kernel to your Jupyter config
ipython kernel install --user --name=your_env_name
Done. You may now use Jupyter notebook under the virtual environment.
jupyter-notebook
Disclaimer: the question has been answered but is hidden in one of the replies. I googled and took sometime to find the right answer. So I just summarize it so someone having the same issue can easily follow.
Assuming that jupyter is installed on your machine, not on the virtual environtment.
Using a virtual environment with Jupyter notebook
VENV_NAME = "YOUR VIRTUAL ENV NAME"
1) virtualenv VENV_NAME
2) source venv/bin/activate
3) Add this package if not present: pip3 install ipykernel
4) Then execute this command: ipython kernel install --user --name=VENV_NAME
5) Now open up the Jupyter Notebook and in change kernel select VENV_NAME
6) To install a new package perform pip3 install <PACKAGE NAME> in your terminal and repeat step 4.
Hope it helps!
Solution without adding a new kernel globally!!
create a new virtual environment by
python3 -m virtualenv envname
Activate your enviroment and install jupyter in it by
pip install jupyter
One thing you have to make sure before installing jupyter is that you don't have following packages already installed in it.
ipykernel
ipython
ipython-genutils
ipywidgets
jupyter
jupyter-client
jupyter-console
jupyter-core
If you've previously installed them then first uninstall them by pip uninstall.
Install your desired packages in activated virtualenv and launch jupyter in it and voila!
Creation of virtualenv with python3 -m venv command
I had the same problem as yours.
In my case I had created the virtualenv with the command
python3 -m venv ./my_virtual_env --system-site-packages
The problem was I could not install jupyter inside the virtual environment as it was already in the system-site-package (when you try to install it, it tells you "Requirement already satisfied").
To install jupyter, (and in a first instance pip, that does not get installed neither in your virtual environment with this command) but still have access to system-site-package you can run :
python3 -m venv ./my_virtual_env
Activate you virtual environment, run pip3 install jupyter (and pip3 install pip) and then turn on the option include-system-site-packages in the file ./my_virtual_env/pyvenv.cfg.
After deactivation and reactivation of you environment, you will have access to system site-packages.
Creation of virtualenv with virtualenv command
Given this answer you can prevent the access to system site-packages by creating a file ./my_virtual_env/lib/python3.4/no-global-site-packages.txt,
and get the access back by removing it.
You can still install jupyter inside your virtual-environment if you have created your virtual env using:
python -m venv --system-site-packages path/to/my-venv
Simply do this:
activate-your-env
pip install -I jupyter
And you are now ready to go
jupyter notebook

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

Categories