2

Getting a very strange error. I am making a virtual environment and initializing it with a pip requirements.txt file, but when I go to run code in the activated environment, the virtual environment interpreter claims to be missing some (and only some) of the modules:

(venv) $ pip list
certifi (2017.7.27.1)
chardet (3.0.4)
decorator (4.1.2)
idna (2.5)
ipython (6.1.0)
ipython-genutils (0.2.0)
jedi (0.10.2)
numpy (1.13.1)
olefile (0.44)
pexpect (4.2.1)
pickleshare (0.7.4)
Pillow (4.2.1)
pip (9.0.1)
prompt-toolkit (1.0.15)
protobuf (3.3.0)
ptyprocess (0.5.2)
Pygments (2.2.0)
PyYAML (3.12)
pyzmq (16.0.2)
requests (2.18.3)
scipy (0.19.1)
setuptools (38.5.1)
simplegeneric (0.8.1)
six (1.10.0)
torch (0.2.0.post3)
torchvision (0.2.0)
tornado (4.5.1)
tqdm (4.15.0)
traitlets (4.3.2)
urllib3 (1.22)
visdom (0.1.5)
wcwidth (0.1.7)
wheel (0.30.0)

So I double check:

(venv) $ pip install tqdm 
Requirement already satisfied: tqdm in ./venv/lib/python3.6/site-packages
(venv) $ python  
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tqdm import tqdm
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tqdm'

people have suggested forcing a reinstall, using a different interpreter source, and just reinstalling. None of these have worked. this is very mysterious. Have any of you seen anything like this? Saw a similar unresolved problem here

UPDATE: Fixed. H/T to @Riverman for helping my find the issue: the problem was that an old unused alias was left over from a while ago, and while pip still had it own point back to python3.6, the python command itself was aliased to some old Anaconda3 version I had lying around somewhere. Alias, I forgot, do not go away by just re-sourcing your .bashrc file, so I ran unalias with the offending python command and it worked!

Would still love to hear from people though if they could explain why this can occur, though. I though venv completely insulated you from the outside environment...is it because aliasing is a system level effect so it seeps into the venv??

physincubus
  • 986
  • 2
  • 11
  • 26

3 Answers3

2

While being inside the virtualenv, please issue the following commands:

pip freeze
pip -V
python -V
which python
which pip

Share your results here to analyze it. I've also experienced pretty similar issues with the requests package before, but that happened on windows to me.

Gal Shahar
  • 2,695
  • 1
  • 21
  • 29
Riverman
  • 503
  • 6
  • 17
  • Output is following: (venv) $ pip freeze  certifi==2017.7.27.1 chardet==3.0.4 decorator==4.1.2 idna==2.5 . . . requests==2.18.3 tqdm==4.15.0 urllib3==1.22 visdom==0.1.5 wcwidth==0.1.7 (venv) $ pip -V pip 9.0.1 from /home/balloch/software/synth-seg/venv/lib/python3.6/site-packages (python 3.6) (venv) $ python -V  Python 3.6.3 :: Anaconda custom (64-bit) (venv) $ which python   python: aliased to /home/balloch/anaconda3/bin/python3.6 (venv) $ which pip  /home/balloch/software/synth-seg/venv/bin/pip This was revealing. didn't realize anaconda was aliased. thoughts? – physincubus Feb 17 '18 at 08:05
  • I'm no expert at anaconda, but afaik, it's using its own binaries to manage python and packages and all sort of things. I suppose you have multiple python and pip installation on your machine and now they conflict with eachother. Edit: I'd look for a pip binary inside anaconda's folder and directly execute that to install the required package. Also, I'd probably remove both anaconda and python and all the packages from my computer (use `createdb` and `locate ` commands to discover them all) and start over to have a clean environment. (check your bash profile too) – Riverman Feb 17 '18 at 08:31
  • It's a good thought, but I don't think removing *all* python distrobutions is a good idea. It's a common understanding that removing the default python packages from Ubuntu can quickly cause things to break, so it is usually advised that you install along-side. I am currently stuck with Ubuntu 14.04.5 because of my company's stack, which has only 2.7.6 and 3.4.3. Thankfully we have all agreed that we are going to sink time into switching to Ubuntu 18.04...April cannot come soon enough – physincubus Feb 17 '18 at 19:53
  • My comment wasn't very accurate -- sorry about that! What I meant was to remove everything related to python that isn't installed by default in your distribution, such as, anaconda and any previously created virtual envs. Don't touch the default python 2.7.x, it can really break things as you say. Afaik 3.x is not installed -by default- on 14.04/16.04, but if your company's modification includes it (and it's not coming from anaconda) you shouldn't touch it. My alternative idea is to manually (without PIP) install `virtualenvwrapper` and create envs using the `-p /usr/bin/python3` switch. – Riverman Feb 17 '18 at 21:08
0

You used pip(which installs for Python 2.7) and you are trying to import the installed package in Python3 so it wont work. You should do pip3 install package-name . pip3 installs for Python3. Install pip3 using apt-get install python3-pip It will work.

The Dead Mayan
  • 357
  • 3
  • 10
  • I'll give that a try, but I don't think that is the case. As per the version shown in the code snippets above, the python version is 3.6.4, as the virtual environment was created with the command: `virtualenv -p python3.6 venv` Confirmed: `$ pip --version  pip 9.0.1 from /home/.../venv/lib/python3.6/site-packages (python 3.6)` – physincubus Feb 17 '18 at 07:12
  • Still the pip tool will respond to Python 2.7, you need to be using pip3. Let me know if it works. – The Dead Mayan Feb 17 '18 at 07:17
  • 2
    confirmed pip3 does not work. Also, for the record what you say is not correct. the pip tool responds to the default python version that it is associated with. as i showed in my previous comment, by printing pip --version, it shows in the output the python it is working with, in this case 3.6 . If I understand the way virtual environments work correctly, while you are in one it is literally impossible for what you say to be true, with the pip tool responding for python2.7, as if you constructed your environment from python3, that is literally the only version of python in the environment... – physincubus Feb 17 '18 at 07:26
  • This Solution points in an absolute wrong direction: Inside an venv the command pip is bound to the venvs pip. In this case pip 9.0.1 – MaKaNu Oct 09 '20 at 11:34
0

I had a very similar problem: I was working on a virtual environment (virtualenv) and installed pandas inside this environment with:

pip3 install pandas

However, when I tried to import this module when working on a jupyter notebook that was also inside this virtual environment with the command:

import pandas as pd

I was getting the error:

ModuleNotFoundError: No module named 'pandas'

Finally, I noticed that, even though I was activating my jupyter notebook from inside my virtual environment, I was opening the jupyter notebook from outside my virtual environment (because I also had this module installed outside of all my virtual environments). My solution was to uninstall jupyter that was outside my virtual environments and when I ran again the juypter nb from inside the desired environment everything worked perfectly.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
robperch
  • 45
  • 1
  • 5