简体   繁体   中英

modules work in python shell but not in jupyter notebooks

since beginning with Python, I have run into this problem multiple times while trying to install and use different packages. In this case, I've been trying to install geopy. I used:

pip3 install geopy

and got :

Collecting geopy
Using cached 
https://files.pythonhosted.org/packages/f3/b4/c8842fc6a07fc95d6887370768850885382b47bf52f5cd8b6a4ae1b6f4d9/geopy-1.17.0-py2.py3-none-any.whl
Collecting geographiclib<2,>=1.49 (from geopy)
Installing collected packages: geographiclib, geopy
Successfully installed geographiclib-1.49 geopy-1.17.0

Next, I tried to make sure it installed, but I got:

geopy --version
geopy: command not found

I tried

sudo apt-get install python-geopy
[sudo] password for jan: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-geopy is already the newest version (1.11.0+ds1-1).
0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.

I also tried using it in Jupyter Notebooks: import geopy

   ---------------------------------------------------------------------------
    ModuleNotFoundError                       Traceback (most recent call last)
    <ipython-input-1-99b9d2081153> in <module>()
    ----> 1 import geopy

    ModuleNotFoundError: No module named 'geopy'

I am not sure, because noob, but I think it might have something to do with the fact Python 2.7 was by default installed with my distribution of Ubuntu, and I installed Python3 and have been using it instead. I added Python = Python3 in the first line of ~/.bashrc in hopes of fixing that. This has happened numerous times before to me with other packages. Please help. Any criticism is welcomed, sorry if I didn't conform to stackoverflow etiquette in any way (still a noob).

I think that Your problem may be fixed with virtual env. Just create Venv, and then use "python" from there. Look here: https://docs.python.org/3/library/venv.html

  1. type 'python -V' in shell and run, see if the default env is your wanted

  2. if you've created virtual env, make sure you created correctly and do not !!! move the env !!!

Try the command as below in your notebook.
Maybe you installed in the different python with Jupiter environment.

!pip3 install geopy

OK I had a bit of a headache with this as I was doing a training program and the guy who was showing did not have it accurate.

Here are some links and suggestions to try to fix. I was using Anaconda for the Jupyter Notebook.

From " https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/pqFuJBDcBb4 " one of the last comments had the command.

So just open Anaconda CLI prompt as Administrator.

conda install -c conda-forge geopy

Once you have done that you have to ensure you are using the right syntax as some of it has changed from the "tutorials" out there. Here is a great explanation: https://github.com/geopy/geopy

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="My_geolocate")

lookingFor = "Moscow"
locations = geolocator.geocode(lookingFor)

print(locations)

RESULTS:

Locating Moscow

Москва, Центральный федеральный округ, Россия

If you want returned location in English:

locations = geolocator.geocode(lookingFor, language="en")

References: https://anaconda.org/conda-forge/geopy https://github.com/geopy/geopy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM