Skip to content Skip to sidebar Skip to footer

PyCharm: ImportError No Module Named X?

X in this example represents any module or package you install. The problem: I have a problem with a package called 'X'. In PyCharm I get an error ImportError: No module named 'X'.

Solution 1:

SHORTER VERSION:

If you have squiggly line below module you import...

No Module found

...move on name of module and press Alt+Enter and select Install package X.

Install package X

This should (probably !!!) install module you thought you installed, but got ImportError.


LONGER:

If you want to use module X:

Go to File -> Settings -> Project:NameOfProject -> Project Interpreter.

Project interpreter image

The window that opens has some specific regions:

  1. This is a project interpreter PyCharm is currently using. You can change you python environments here also. If you want to add virtual environment you created manually, continue reading.
  2. A little "cog" or a "gear" is used to Add, Edit or Remove environments. If you want to add virtual environment you created manually, select Add... when gear icon is clicked, and make sure to set proper path to python.exeof your virtual environment.
  3. IMPORTANT: A list of all installed packages, represented by: Package name and Version. If you tried to use package, but got ImportError no module named 'X', make sure to check if package is listed here!!!. If not, it's not installed in the current python environment and it should be installed (continue reading).
  4. A little + represents Install. It can be used to install packages. Simply click on + sign, search for a package and click Install Package at the bottom after you found it. You can also specify the version you want of a package. In example below, we searched for flask package.

    Package installation A package should be installed and listed now in installed packages.


Solution 2:

PyCharm creates a virtual environment with its own Python Interpreter for your project, you need to install the module for the interpreter you are using. To do this go to Settings -> Project: yourProjectName -> Python Interpreter, click the plus icon and select the module you would like to install. Alternatively you could force PyCharm to use your other Interpreter by selecting it from the dropdown at the top of the Python Interpreter settings page I mentioned before.


Solution 3:

There are many reasons for this. The reason and fix I'm going to put here is extremely rare, just decided to put it hoping at least a single person can get help from this answer.

The issue

PyCharm treats __init__.py as a non python file.

init.py is not treated as a python file

This happens when you forgot to use .py extension in your code's __init__.py files, and then you add the extension later. Then pycharm starts treating all __init__.py files (even external library files) as non python. How strange?

Detect if the issue is this

Scroll click on the library name (not the module name).

Or right click and goto Declaration or Usages

goto declaration

You will see the library's __init__.py as plain text.

Code is in plain ext

If you see the code as plain text, that's the issue!

The Solution

Find the file location in pycharm navigation. If you are using virtual environment, the file fill be in venv/lib/site-packages/{library_name}.

Right click on __init__.py, click override file type. Select python.

override file type

You'll see the error is gone!


Post a Comment for "PyCharm: ImportError No Module Named X?"