Have you installed a Python package, but now you can’t import it? Or can you import it when you run code one way, but not when you run code another way?
These are common problems, and this post is an attempt to explain what’s going on and how you can fix it. If someone has sent you a link here, they’re not trying to be rude: we can help more people more effectively by sharing a detailed explanation than by writing a short explanation each time.
Lots of Python
There’s probably more than one copy of Python on your computer. When you run code in different ways, it might get run by different Pythons. To see which Python is running your code, run:
all of these are different Pythons (and they aren’t all of them):
Each conda environment, virtualenv or pyenv you create is a separate Python. But you can also have different names for the same Python — e.g.
/usr/bin/python3 is the same as
/usr/bin/python3.5 on my laptop (it’s a
Each Python installation or environment has its own package folder. When you install a package, it’s being installed for one particular Python, which might not be the one you are trying to load it from.
You can also see all of the places that import will look for a module by running:
What can I do?
First, use the code above to find which Python is running your code.
Install the package for the same Python you’re using.
With pip, this is easy:
Switch to use a Python where the module you want is installed.
It’s hard to provide instructions for this, because it depends on the tools you’re using. If you’re using an IDE, look in the settings. Here it is in Pycharm, for instance:
hope this can help you , Let me know if you have any questions or comments.