4

I'm trying to install pymongo in a virtual environment, and pip says that it's installed, but it doesn't appear to be:

(venv)$ ./myapp.py
Traceback (most recent call last):
  File "./myapp.py", line 5, in <module>
    import myAppMongo
  File "/home/me/python/myapp/myAppMongo.py", line 4, in <module>
    from pymongo import MongoClient
ImportError: No module named 'pymongo'

I've installed pymongo:

(venv)$ pip --no-cache install pymongo
Collecting pymongo
  Downloading pymongo-3.2.tar.gz (473kB)
    100% |████████████████████████████████| 475kB 18.3MB/s
Installing collected packages: pymongo
  Running setup.py install for pymongo ... done
Successfully installed pymongo

But it doesn't appear in pip's freeze output:

(venv)$ pip freeze |grep -ic pymongo
0

I'm running Amazon's linux with their python 3 packages:

(venv)$ sudo yum list installed|grep -i python3
python34.x86_64                   3.4.3-1.30.amzn1              @amzn-main
python34-devel.x86_64             3.4.3-1.30.amzn1              @amzn-main
python34-libs.x86_64              3.4.3-1.30.amzn1              @amzn-main
python34-pip.noarch               6.1.1-1.21.amzn1              @amzn-main
python34-setuptools.noarch        12.2-1.30.amzn1               @amzn-main
python34-test.x86_64              3.4.3-1.30.amzn1              @amzn-main
python34-tools.x86_64             3.4.3-1.30.amzn1              @amzn-main
python34-virtualenv.noarch        12.0.7-1.12.amzn1             @amzn-updates

How can I get this working?

  • Does `./myapp.py` define an interpreter on its first line? – Peter Wood Jan 28 '16 at 00:00
  • What if you try `python myapp.py`? – Peter Wood Jan 28 '16 at 00:01
  • Yes, `$ head -n 1 myapp.py #!/home/me/python/myapp/venv/bin/python` – IgniFerroque Jan 28 '16 at 03:58
  • @PeterWood, either way, I get the `No module named 'pymongo'` error. – IgniFerroque Jan 28 '16 at 04:01
  • I don't see the `(venv)` prefix in your `$ pip freeze [...]` example. Did you run the command from the virtual environment and forgot to copy the prefix or did you not run it from the virtual environment? – Niklas R Jan 28 '16 at 09:19
  • What happens if you just run python and do ```import pymongo``` from interactive interpreter prompt? – Mad Wombat Jan 28 '16 at 15:28
  • @NiklasR I did, and I forgot to include it. I've edited it in to avoid future confusion. – IgniFerroque Jan 28 '16 at 23:11
  • @MadWombat I get the same `No module named 'pymongo'` error. – IgniFerroque Jan 28 '16 at 23:12
  • As people mentioned, chances are you are trying to use pip from a different python version. You want to make really sure. Do `which pip`, look at the pip file and check that they python binary in its first line is the same as the one returned by `which python`. Then try to use full path to pip to install pymongo, run python using full path and try `import pymongo` again. See if you find a problem in the process. – Mad Wombat Jan 29 '16 at 18:03
  • @MadWombat `(venv)$ head -n 1 $(which pip) #!/home/me/python/myapp/venv/bin/python3.4` I have run them with the full paths to my virtual environment and I get the same behavior as above. – IgniFerroque Feb 03 '16 at 19:13
  • FWIW, using easy_install worked fine :\ – IgniFerroque Feb 03 '16 at 19:14
  • @MadWombat also I've installed several other packages in the virtual environment and they all worked as expected. – IgniFerroque Feb 03 '16 at 19:31
  • @IgniFerroque I have the exact same issue with the package `lsm-db` on my ec2. I asked it [here](https://stackoverflow.com/questions/57105227/cannot-pip-install-package-in-virtualenv-on-ec2). Did you ever find the reason and the solution? – Logan Yang Jul 19 '19 at 15:26
  • I never found the solution :( – IgniFerroque Jul 20 '19 at 17:17

3 Answers3

1

Try using pip3 to install pymongo instead of pip!

kevchoi
  • 434
  • 2
  • 7
  • It didn't seem to make a difference, I think they're both the same thing. Running `which pip` and `which pip3` shows they're both in the bin folder of my virtual environment. Also, `pip --version` and `pip3 --version` shows the same version. – IgniFerroque Jan 28 '16 at 03:47
  • 1
    and you're running these commands in the virtual environment, correct? – kevchoi Jan 28 '16 at 04:09
  • `#!/home/me/python/myapp/venv/bin/python3`? – kevchoi Jan 28 '16 at 23:16
  • `(venv)$ head -n 1 $(which pip) #!/home/me/python/myapp/venv/bin/python3.4` – IgniFerroque Feb 03 '16 at 19:14
0

Let’s start from the beginning:

Type this and paste here the response:

type pip
0

I wonder if your virtual environment was somehow not set up correctly?

do both

$ which python

and

$ which pip

Show you that the commands are being run from within the venv?

Myk Willis
  • 12,306
  • 4
  • 45
  • 62