2

I am using VS and I am trying to run geopy, I installed all the prerequisites and get this error "ModuleNotFoundError: No module named 'geopy.geocoders'; 'geopy' is not a package"

from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="http")
location = geolocator.geocode("175 5th Avenue NYC")
print(location.address)
print((location.latitude, location.longitude))
print(location.raw)

What am I missing?

IguanaBot
  • 23
  • 1
  • 5
  • Did you install geopy through `pip install geopy`? – Psychotechnopath Apr 16 '21 at 18:17
  • Yes it says already satisfied – IguanaBot Apr 16 '21 at 18:19
  • In that case, make sure VS code is using the right python interpreter, or you are installing the package in the correct `venv` or `conda` environment (The python installation VS-code is using). I've tried your code on my machine and doesn't result in any errors. – Psychotechnopath Apr 16 '21 at 18:21
  • Sorry if it is a stupid question, I am a noob with vs and python, when I check the python interpreter I only see one option "Python 3.9.1 64 bit -\appdata\local\programs\python39\python.exe" do I need to add venv or conda? – IguanaBot Apr 16 '21 at 18:27
  • No, not perse although it has some advantages to work with environments. See for example: https://www.freecodecamp.org/news/why-you-need-python-environments-and-how-to-manage-them-with-conda-85f155f4353c/ or https://medium.com/datareply/working-with-python-environments-anaconda-package-manager-and-ides-663e771b6ed8. Chances are that you have multiple installations of python installed. Can you open a command prompt and type `where python` and share results? – Psychotechnopath Apr 16 '21 at 18:29
  • When I run where python in cmd it says" could not find files for the given pattern" – IguanaBot Apr 16 '21 at 18:32
  • How did you run `pip install geopy`? Through venv or through command prompt? What happens if you cd (`cd "C:/PATH_TO_PYTHON`) to directory where python is installed, and type `python` ? – Psychotechnopath Apr 16 '21 at 18:40
  • I am fixing python to work on cmd first and I will update after that – IguanaBot Apr 16 '21 at 18:43
  • I ran " py -m pip install geopy" on the VS terminal – IguanaBot Apr 16 '21 at 18:44

2 Answers2

2

I suspect the problem lies in you installing the geopy package in wrong version of python (The one that comes pre-installed in \AppData\Local\Microsoft\WindowsApps\python.exe is not full install). Grab a version of python (Either anaconda or vanilla python from python website). Let it install in default location, and then point VS code version of python that comes pre-installed with windows. Install geopypackage thorugh pip install geopy, either with VS, or through cmd with conda or pip. This should fix your problem.

Psychotechnopath
  • 2,471
  • 5
  • 26
  • 47
  • Thank you so much for you help, I did install Anaconda and install geopy by accessing CMD with Anaconda navigator but I still get this error, I do see when I open VS an alert "The environment variable 'Path' seems to have some paths containing characters (';', '"' or ';;'). The existence of such characters are known to have caused the Python extension to not load. If the extension fails to load please modify your paths to remove these characters." I tried to find how to solve this path issue but I really don't know what the problem is, i checked the paths and it looks good. – IguanaBot Apr 16 '21 at 21:19
  • Where did you install Anaconda? Please paste the complete path. Did you create environment? Did you add anaconda to path when installing? – Psychotechnopath Apr 17 '21 at 10:28
  • Thank you, after installing Anaconda and creating an environment it worked! – IguanaBot Apr 22 '21 at 16:21
  • By the way I have another question maybe you will know the issue https://stackoverflow.com/questions/67217284/geopy-scraping-yellow-pages-returns-data-but-with-partial-coordinates – IguanaBot Apr 22 '21 at 17:45
2

This error: ModuleNotFoundError: No module named 'geopy.geocoders'; 'geopy' is not a package may also occur if you have named the main program file you created as geopy.py and try to run it as python geopy.py or another file has the name geopy.py in the same folder from which you run your program. Python will consider your program file as a module and try to find something in it that is naturally not in it. About where the Python is looking for modules, see sys.path.

In this case, rename your program file so that its name does not equal with the name of the imported module.

kirogasa
  • 627
  • 5
  • 19