ImportError after successful pip installation [duplicate]
Asked Answered
P

2

44

I have successfully installed a library with pip install <library-name>. But when I try to import it, python raises ImportError: No module named <library-name>. Why do I get this error and how can I use the installed library?

Pepi answered 20/9, 2015 at 13:45 Comment(19)
@BhargavRao Yikes. I'm not entirely on-board with the duplicate closure here, simply because the question at the dupe target is a horrible mess full of UPDATE and EDIT sections. Tonight I'll see if I can tidy it up. If I can, then I think it'll probably make sense to merge the questions so that cel's (good) answer from here can get exposed to the larger audience of the other question.Alidaalidade
@MarkAmery, I tidied it up by a bit to reduce you some work tonight. I will also do a bit of search to see if there are any other canonicals to which both of them can be hammered.Oval
@BhargavRao One idea - perhaps mischievous - would be to use this as the canonical, merge the other question into this one, and edit to swap the titles around (since the evidence seems to show that the other question's title is better for SEO).Alidaalidade
That seems a good option too @MarkAmery, the answer here is anytime better than the ones there. (This probably needs some editing). However, I'm a bit concerned about merging them, as the other post seems to be about one particular library. We would need to update most of the answers to make them library agnostic. It can be done, but needs some effort. I also think that it probably is a good time to involve the Python chat room in here, for their views.Oval
@BhargavRao I haven't scrutinised the other post too much yet, but a quick CTRL-F reveals that, although the question purports to be specifically about the mechanize library, that library is in fact not mentioned in a single one of the answers. That makes me optimistic, at first glance, that they'd all apply fine here.Alidaalidade
I agree that the dupe target is suboptimal, but this Q&A is also a bit confusing. The dupe target I usually use for this problem is this one. Clear question, clear answer. But harder to find if you're facing this in an XY problem (so good dupes are always welcome).Alasteir
@MarkAmery In that case, it probably would be easier. I will read both the posts once tonight (UTC tomorrow), and then get back to you on this.Oval
@AndrasDeak, thanks, that seems to be a more comprehensive target for this particular question, given that the answers also point to the same solution. I'm now thinking of a different way to dupe this question to your target, and leave the other question alone.Oval
I don't think @AndrasDeak's target is a valid dupe target for either of the other two. This question (as I interpret it and would like to see it used) is a catchall question for all possible reasons that you might not be able to import a library after having just successfully installed it. Multiple Python versions are only one such possible cause (albeit the one that the main answer here currently discusses exclusively).Alidaalidade
@MarkAmery if you successfully import a library you can import it. One weird exception is the current dupe target where OP had some weird permission issue. What use cases do you have in mind that aren't actually due to OP being confused about which pip they are using?Alasteir
@AndrasDeak Another possibility is given in the second answer on this question; a Python newbie may fall into the trap of assuming that the project name used in their Pip install command must be identical to the module name they should use to import the module, which isn't necessarily the case.Alidaalidade
@AndrasDeak There's also option 3 at https://mcmap.net/q/76297/-unable-to-import-a-module-that-is-definitely-installed.Alidaalidade
@MarkAmery I guess some of those are valid, thanks. But the original goal of this Q&A seems to have been to give a verbose explanation for the dupe target I suggested. I'm not sure sending users with different esoteric installation problems here would help them. A standalone post where these weird cases are explained might be more useful.Alasteir
@AndrasDeak Then there's all the answers there suggesting fiddling with PYTHONPATH. I'm not knowledgable enough to know if those are valid; do they all indicate that really the wrong Pip was used in the first place? Even if those answers aren't valid, we're already up to 4 entirely distinct causes (wrong Pip, wrong permissions, module name doesn't match project name, and shadowing by another package) for the "ImportError after successful install" phenomenon, only one of which is covered in your dupe.Alidaalidade
@AndrasDeak That's a legit criticism, and it'd be nice to be able to somehow curate the answers down on this question or the previous dupe target to just 4, each succinctly covering one of these cases.Alidaalidade
@BhargavRao I've gone at https://mcmap.net/q/76297/-unable-to-import-a-module-that-is-definitely-installed/1709587 with a chainsaw; most of the details were, I think, ultimately unnecessary. I'd personally now recommend merging this question into that one, which I think is a better dupe target than the one this is currently closed against, and perhaps editing cel's answer to contain a link to Andras's preferred dupe.Alidaalidade
@Mark, Cool, I'll change the duplicate to that, but I'm still not certain if a merge is needed here. (Merging is a small pain as it is almost irreversible). The OP seems to be active too. I think it is better to wait for a few more users to confirm once.Oval
@BhargavRao I agree that waiting to give the OP here and the Python chat room denizens a chance to weigh in is a good idea.Alidaalidade
@BhargavRao, and all in the conversation: I am generally fine with improving by merging duplicates. I think it's important that the question/answer pair is available in some form in an easy searchable way. I will trust in your judgement on how to merge/improve the question.Pepi
P
106

TL;DR: There are often multiple versions of python interpreters and pip versions present. Using python -m pip install <library-name> instead of pip install <library-name> will ensure that the library gets installed into the default python interpreter.

Please also note: From my personal experience I would advice against using sudo pip install to install packages into system's default python interpreter. This can lead to a various messy issues. Whenever you are tempted to call pip with sudo, please check first if a virtualenv is not a better option for you.


Most modern systems ship multiple python interpreters. Each interpreter maintains its own set of installed packages. When installing new packages, it is important to understand into which interpreter those packages are actually installed.

On unix systems the shell can be used to understand what exactly is happening.

Typing which -a python shows all interpreters that in your PATH. The first line corresponds to the interpreter that is used when you run python from the command line.

/private/tmp/py32/bin/python
/usr/local/bin/python
/usr/bin/python

Each pip version belongs to exactly one interpreter. which -a pip shows all pip versions. Again the first line is what will be called when you type pip in your shell.

/usr/local/bin/pip
/usr/bin/python

Note that in this case python belongs to the interpreter installed in /private/tmp/py32/, but pip installs into the interpreter /usr/local/bin. After a successful install of a library, you will not be able to import it in your default python interpreter.

So how do you import the installed library?

Your first option is to start the desired interpreter with its full path. So if you type /usr/local/bin/python, you will be able to import the library.

The second - often preferred - option is to specifically invoke the right version of pip. To do so, you can use python -m pip install <library-name> instead of pip install <library-name>. This will call the pip version that belongs to your default python interpreter.

Pepi answered 20/9, 2015 at 13:45 Comment(11)
what modern systems have multiple version of the same python? When you see /usr/local/bin/python that usually means you installed it.Lindesnes
the point I am trying to make is that there's not just python, but multiple python interpreters. Often a python2.x and a python3.x interpreter by default. Hmh, maybe I have to rewrite to make my point clearer.Pepi
Yes but on unix there are the default system python interpreters not multiple versions of python2 or 3 interpreters installed by default, if you see other interpreters then you installed them, which -a python should return one path on a fresh install and it should be made very clear that you should not screw with the default python. A virtualenv or pyenv would be the way to go if you don't know what you are doing.Lindesnes
@Padraic Cunningham, agree that using virtualenvs instead of touching system's python is the best way to go. Yet many still use sudo pip install out of convenience. I would not consider installing into systems python as horrible. After all, probably we all did before virtualenvs were introduced.Pepi
I use pyenv when I want to run my code against different versions of python or test out new features, I use the system python for everything else because I have a good understanding of how everything works but that understanding came the hard way and involved breaking my OS a couple of time over the years, using the system python when you know how is fine but there are numerous posts on SO like why is apt-get not working anymore.. from people who installed multiple versions of python and then changed their default in an attempt to rectify ImportError: No module named <library-name>Lindesnes
@PadraicCunningham, I will think about adding a disclaimer that points to virtualenvs. People should have at least a reference where they find the preferred way to avoid breaking their system. Do you know of a question-answer pair that explains why sudo pip install is evil and how virtualenv's are set up to avoid messing up system's python?Pepi
@celeo, not off the top of my head, I don't think sudo pip install is evil, I think installing multiple versions of the same python is the problemLindesnes
Let us continue this discussion in chat.Pepi
for me >>> sudo chown -R $USER /Library/Python/2.7 >>> python -m pip install <module> made it working :)Gibraltar
which -a python /home/coldshot/anaconda3/bin/python /usr/bin/python /bin/python Uninstall mtcnn if already installed and import error occurs $ sudo -H pip uninstall mtcnn $ python -m pip install mtcnn Collecting mtcnn Installing collected packages: mtcnn Successfully installed mtcnn-0.0.9Stricklin
So here after specifically invoking python from the virtualenv directory, if the module is not imported then does that mean it's not installed altogether? The irony is that when I do a pip freeze / pip list it shows me the module as installed with the version so this becomes confusing.Yesterday
L
2

A couple more points:

  1. Check to see if you're installing the library into the virtualenv that you want to use.
  2. There are some libraries whose package names are different from the library's name. You could take a look at their documentation online (google with keyword python <library> would usually bring up the information) to see if you're importing the package correctly.
Ludivinaludlew answered 20/9, 2015 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.