Installing a pip package from within a Jupyter Notebook not working - python

When I run !pip install geocoder in Jupyter Notebook I get the same output as running pip install geocoder in the terminal but the geocoder package is not available when I try to import it.
I'm using Ubuntu 14.04, Anaconda 4.0.0 and pip 8.1.2
Installing geocoder:
!pip install geocoder
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting geocoder
Downloading geocoder-1.15.1-py2.py3-none-any.whl (195kB)
100% |████████████████████████████████| 204kB 3.2MB/s
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): ratelim in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): click in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): decorator in /usr/local/lib/python2.7/dist-packages/decorator-4.0.10-py2.7.egg (from ratelim->geocoder)
Installing collected packages: geocoder
Successfully installed geocoder-1.15.1
Then try to import it:
import geocoder
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-603a981d39f2> in <module>()
----> 1 import geocoder
ImportError: No module named geocoder
I also tried shutting down the notebook and restarting it without any luck.
Edit: I found that using the terminal installs the geocoder package in /home/ubuntu/.local/lib/python2.7/site-packages and using a notebook installs it in /usr/local/lib/python2.7/dist-packages which is not in the path. sys.path.append('/usr/local/lib/python2.7/dist-packages') solves the problem for the current session.
So how can I permanently modify the path or tell pip where to install geocoder?

In IPython (jupyter) 7.3 and later, there is a magic %pip and %conda command that will install into the current kernel (rather than into the instance of Python that launched the notebook).
%pip install geocoder
In earlier versions, you need to use sys to fix the problem like in the answer by FlyingZebra1
import sys
!{sys.executable} -m pip install geocoder

! pip install --user <package>
The ! tells the notebook to execute the cell as a shell command.

%pip install fedex #fedex = package name
in 2019.
In older versions of conda:
import sys
!{sys.executable} -m pip install fedex #fedex = package name
*note - you do need to import sys

In jupyter notebook under python 3.6, the following line works:
!source activate py36;pip install <...>

The problem is that pyarrow is saved by pip into dist-packages (in your case /usr/local/lib/python2.7/dist-packages). This path is skipped by Jupyter so pip won't help.
As a solution I suggest adding in the first block
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
or whatever is path or python version. In case of Python 3.5 this is
import sys
sys.path.append("/usr/local/lib/python3.5/dist-packages")

This worked for me in Jupyter nOtebook /Mac Platform /Python 3 :
import sys
!{sys.executable} -m pip install -r requirements.txt

Try using some shell magic: %%sh
%%sh pip install geocoder
let me know if it works, thanks

Alternative option :
you can also create a bash cell in jupyter using bash kernel and then pip install geocoder. That should work

I had the same problem.
I found these instructions that worked for me.
# Example of installing handcalcs directly from a notebook
!pip install --upgrade-strategy only-if-needed handcalcs
ref: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
Issues may arise when using pip and conda together. When combining
conda and pip, it is best to use an isolated conda environment. Only
after conda has been used to install as many packages as possible
should pip be used to install any remaining software. If modifications
are needed to the environment, it is best to create a new environment
rather than running conda after pip. When appropriate, conda and pip
requirements should be stored in text files.
We recommend that you:
Use pip only after conda
Install as many requirements as possible with conda then use pip.
Pip should be run with --upgrade-strategy only-if-needed (the default).
Do not use pip with the --user argument, avoid all users installs.

conda create -n py27 python=2.7 ipykernel
source activate py27
pip install geocoder

Using pip2 worked for me:
!pip2 install geocoder
...
import geocoder
g = geocoder.google('Mountain View, CA')
g.latlng
[37.3860517, -122.0838511]

Related

pip list returns -bash: pip: command not found

I am checking the documentation and there are basically two ways to install pip.
python -m ensurepip --upgrade
Looking in links: /var/folders/d0/gnksqzwn2fn46fjgrkp6045c0000gn/T/tmpe2ll6upv
Requirement already satisfied: setuptools in /Applications/Anaconda/anaconda3/lib/python3.9/site-packages (58.0.4)
Requirement already satisfied: pip in /Applications/Anaconda/anaconda3/lib/python3.9/site-packages
All the packages are in /Applications/Anaconda/anaconda3/lib/python3.9/site-packages directory including pip.
However, when I run pip list from base environment, I get:
-bash: pip: command not found
I am getting module not found error when running my application.
My application was running fine until I tried to upgrade a python package. What did I mess up? What do I need to check?
Try this python -m pip list instead of python pip list
It could be that pip might not be installed or it could be the version of the pip might not be compatible with the current version.
pip used for python2, use pip3 for python3 and upgrade your pip and retry once.
python -m pip install --upgrade pip

ImportError: No module named 'pandas' (inside virtualenv)

I created a virtual environment named quora for python.
I installed wheel and then pandas as instructed.
I cant get pandas to work for some reason.
Can someone help me.
I have tried all the other solutions available to similar questions on this website. Still no use.
(quora) (jessie)griffith#localhost:~/environments$ sudo pip install wheel
Requirement already satisfied: wheel in /usr/lib/python2.7/dist-packages
(quora) (jessie)griffith#localhost:~/environments$ sudo pip install pandas
Requirement already satisfied: pandas in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: numpy>=1.7.0 in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: python-dateutil in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from python-dateutil->pandas)
(quora) (jessie)griffith#localhost:~/environments$ python getdata.py
Traceback (most recent call last):
File "getdata.py", line 2, in <module>
import pandas as pd
ImportError: No module named 'pandas'
I had this problem in a virtualenv with pip3 and pandas, tried all these previous answers, none of which actually work. but you can use easy_install pandas. et voilà.
Don't use sudo in a virtualenv — sudo pip install installs packages into global site-packages, not in virtualenv.
Either install pandas in the virtual environment (pip install after activating venv) or enable access to the global packages (recreate venv with option --system-site-packages or use command toggleglobalsitepackages from virtualenvwrapper).
I had the same problem. I fixed it by deleting my virtualenv directory and creating a new environment.
Check "which python" you are running using that command. You may need to export PATH to the python env instead of your default python which might be /usr/lib/bin. It might be installed in your quora env but the python that is being picked up is different and that doesn't have pandas
go to pyvenv.cfg and change
include-system-site-packages = false
to
include-system-site-packages = true
I tried all the answers before in my archlinux machine but none worked. But this post from Nikolai Janakiev helped me find a good solution.
Solution
Don't forget to first activate the virtual env, mine is named .venv:
$ source .venv/bin/activate
(.venv) $ python3 -m pip install ipykernel
Pick any arbitrary name and replace with NEW_KERNEL, this shows up in your jupyter notebook with the same name:
(.venv) $ python3 -m ipykernel install --name=NEW_KERNEL
And you're done!
Creating Virtual Environments
If you're not already familiar with setting up a virtual environment, here is the official guide.
Let's suppose you want to create a virtual environment under the name .venv. I use the . prefix to hide it by default.
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python3 -m pip install pandas
And voila! You have access to the pandas package in .venv environment.
Using Virtual Environments in Jupyter Notebooks
If you're wondering how to activate the virtual environment in a jupyter-notebook, just follow the Solution section above, and then open up the notebook, and click on Kernel then Change Kernel to NEW_KERNEL or the name you picked in the Solution section.

Python packages hash not matching whilst installing using pip

I am using pip to install all my python packages but get error as shown in the trace below. What is the problem and how can I solve it?
usr#comp:~$ pip install flask
Collecting flask
Using cached Flask-0.11.1-py2.py3-none-any.whl
Collecting itsdangerous>=0.21 (from flask)
Using cached itsdangerous-0.24.tar.gz
Collecting click>=2.0 (from flask)
Using cached click-6.6.tar.gz
Collecting Werkzeug>=0.7 (from flask)
Using cached Werkzeug-0.11.11-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): Jinja2>=2.4 in /usr/lib/python2.7/dist-packages (from flask)
Requirement already satisfied (use --upgrade to upgrade): MarkupSafe in /usr/lib/python2.7/dist-packages (from Jinja2>=2.4->flask)
THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
Werkzeug>=0.7 from https://pypi.python.org/packages/a9/5e/41f791a3f380ec50f2c4c3ef1399d9ffce6b4fe9a7f305222f014cf4fe83/Werkzeug-0.11.11-py2.py3-none-any.whl#md5=c63a21eedce9504d223ed89358c4bdc9 (from flask):
Expected md5 c63a21eedce9504d223ed89358c4bdc9
Got 13a168aafcc43354b6c79ef44bb0dc71
There is a similar problem (Why does pip fail with bad md5 hash for package?) from 2013 the solution that I tried that worked for me is this:
sudo pip install --no-cache-dir flask
given by attolee
The problem here is the Python package was updated with new hash value while pip was trying to install the Python package using the old hash value cached in pip cache directory. This cache needs to be purge before the pip install attempt. So the full solution is:
python -m pip cache purge
python -m pip install <package>
--no-cache-dir did not work for me in raspberry pi 4 at first.
Found that the problem was due to unexpected network change/failure during pip installation
I had to download the broken .whl file manually with wget
and install it like below:
sudo pip install scipy-1.3.0-cp37-cp37m-linux_armv7l.whl
followed by
sudo pip install --no-cache-dir keras
Then it worked.
Looks like a cache problem, the cached package is different from REQUIREMENTS.
Perhaps caused by last updates interruption.
I did this which fixed my problem:
rm ~/.cache/pip -rf
You need to upgrade your pip into the newer version:
Using this command:
python -m pip install -upgrade pip
for Mac/Linux operating system and use
python -m pip install --upgrade tensorflow
for Windows to update your pip. Then run your command
pip install flask
In case you got this error while using pipenv try
$ pipenv --clear
$ pipenv lock
$ pipenv install
first, try to upgrade your pip then install the library
python -m pip install -upgrade pip
if it didn't work just try to install it without the cash
pip install --no-cache-dir the_library_name
I got the error during installing panads
You need to remove the cache and reinstall .
pip install --no-cache-dir flask
I had a similar issue for a different module. It was caused by network failure. My fix was nothing complex but another attempt at installing it and it worked.
maybe pipiserver(where you pip install from) upload a pkg for example flask-1.0.0.tar.gz, and rm is upload a new flask-1.0.0.tag.gz,if new pkg code has changed ,the hash must be different,there is two ways:
installl an older pkg version =, pip install flask==0.0.9
wait new pkg release flask==1.0.1 or cache expiration.
I have tried to clear pip cache with "-m pip cache purge" and using the "--no-cache-dir" argument but it was not helping.
In my case it was VPN being active during the attempts to install the package. As soon as I have turned it off everything worked as expected.

Python import fails Openpyxl

I know many questions speak about this problem, but i tried a lot and didn't fin any solution to my "very classical" issue : Python import fails altough package installed:
MacBook-Pro-de-Stephanie:scripts user$ sudo -H pip install openpyxl
Collecting openpyxl
Requirement already satisfied (use --upgrade to upgrade): jdcal in /usr/local/lib/python2.7/site-packages (from openpyxl)
Requirement already satisfied (use --upgrade to upgrade): et-xmlfile in /usr/local/lib/python2.7/site-packages (from openpyxl)
Installing collected packages: openpyxl
Successfully installed openpyxl-2.3.5
Package installed right ?
MacBook-Pro-de-Stephanie:scripts user$ python interactionsXLSX2CSV.py
Traceback (most recent call last):
File "interactionsXLSX2CSV.py", line 5, in <module>
from openpyxl import load_workbook
ImportError: No module named openpyxl
Fails right ?
MacBook-Pro-de-Stephanie:scripts user$ echo $PYTHONPATH
/usr/local/lib/python2.7/:
Path seems ok, no ?
I set permissions with:
sudo chmod -R ugo+rX ./lib/python2.7/site-packages/
What can i try else ?
Thank you very much
Here was the solution for me (linked to Mac OS clearly) : Can't load Python modules installed via pip from site-packages directory
/usr/bin/python is the executable for the python that comes with OS
X. /usr/local/lib is a location for user-installed programs only,
possibly from Python.org or Homebrew. So you're mixing different
Python installs, and changing the python path is only a partial
workaround for different packages being installed for different
installations.
In order to make sure you use the pip associated with a particular
python, you can run python -m pip install <pkg>, or go look at what
the pip on your path is, or is symlinked to.
I am not getting that error. Just now i installed. All i am getting is
" /usr/local/lib/python2.7/dist-packages/openpyxl/xml/init.py:15: UserWarning: The installed version of lxml is too old to be used with openpyxl
warnings.warn("The installed version of lxml is too old to be used with openpyxl")"
Once try by using below command.
sudo apt-get install python-openpyxl

How to install virtualenv scripts to /usr/bin

I install virtualenv with command sudo /usr/bin/pip-2.6 install virtualenv
And it says
Requirement already satisfied (use --upgrade to upgrade):
virtualenv in /usr/local/lib/python2.6/dist-packages
Cleaning up...
Why pip from /usr/bin looks to /usr/local/lib?
I need to install virtualenv scripts directly to /usr/bin, so I write
sudo /usr/bin/pip-2.6 install --install-option="--install-scripts=/usr/bin" virtualenv
But again it responds with
Requirement already satisfied (use --upgrade to upgrade):
virtualenv in /usr/local/lib/python2.6/dist-packages
Cleaning up...
Adding --upgrade doesn't help.
How can I install virtualenv scripts to /usr/bin ?
For your current issue can you first uninstall virtualenv using pip, and then reinstall using --script-dir=DIR, -s DIR as an --install-option.
As for your issue running pip with extra arguments and install not seeming to do anything, that may be fixed on develop branch at https://github.com/pypa/pip if not please file a bug with us on the GitHub issue tracker for pip.
As to why it is behaving like this - the install is based on the python installation not on the location of pip. Pip uses setuptools/distribute under the hood and conforms to the configuration of the python it is running under.
You can see where the version of python you are using installs to by running it - in this case probably python2.6 and querying the sys module.
>>> import sys
>>> sys.prefix
>>> sys.exec_prefix
See also distutils.sysconfig
For more information:
Installing Python Modules
setuptools
distribute
Try:
sudo /usr/bin/pip-2.6 install --install-option="--prefix=/usr/bin" virtualenv
(source).

Categories