'No module named 'geopy'

I am trying to install geopy in a virtual environment created by miniconda. When I do pip install geopy, it gives me this:

(finalenv) MacBook-Pro ~ % pip install geopy
Requirement already satisfied: geopy in /usr/local/Caskroom/miniconda/base/envs/finalenv/lib/python3.6/site-packages (2.2.0)
Requirement already satisfied: geographiclib<2,>=1.49 in /usr/local/Caskroom/miniconda/base/envs/finalenv/lib/python3.6/site-packages (from geopy) (1.52)

The python version for this Finalenv is Python 3.6.13 :: Anaconda, Inc.

However, when I try to run my script:

import psycopg2

import geopy

I get an error that:

(finalenv) MacBook-Pro ~ % /usr/local/bin/python3 /Users/s/Desktop/s-project/v2.py
Traceback (most recent call last):
  File "/Users/s/Desktop/s/file.py", line 15, in <module>
    import geopy
ModuleNotFoundError: No module named 'geopy'

what am I missing here?



Solution 1:[1]

Since you are using Anaconda, you can try installing it through conda to see if the installation happens on the same folder. Open Anaconda CLI prompt as Administrator. You can try

conda config --add channels conda-forge

and

conda install -c conda-forge geopy.

Solution 2:[2]

/usr/local/bin/python3 is your OS python executable, not the one used by your virtualenv.

In order to run the script always with the python3 on the virtual environment (if it is activated), you can do this:

  1. Add #!/usr/bin/env python3 at the beginning of the file. It will look like this:
#!/usr/bin/env python3

import psycopg2
import geopy

  1. Give execution permissions to the file - in your case: chmod u+x v2.py

  2. And run the file from the shell: ./v2.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ka-Wa Yip
Solution 2 Iñigo González