Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PyCharm complaining on missing modules when using absolute imports?

Tags:

I have the following package structure:

projects/     package_name/         __init__.py         model.py 

In __init__.py I have the following import statement:

import package_name.model as model 

PyCharm complains here that there is no module named package_name. But when I import the package from the terminal while standing in projects/, Python imports the package without problems. My working directory in PyCharm is as well projects, and I have added it to my PYTHONPATH.

Why is PyCharm complaining despite it seems to work just fine, and how do I fix it?

like image 873
Jimmy C Avatar asked Jul 04 '14 23:07

Jimmy C


People also ask

Why can't I import modules in python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How do I fix an import error in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

How do I import a module into PyCharm?

Type the name of the package and hit Alt-Enter , then choose Install and Import package . PyCharm will do both: you'll see a notification during the installation, then the import will be generated in the right way, according to your project styles.


1 Answers

PyCharm needs to know which files in your tree are Python sources in order for it to figure out your module structure.

Open File -> Settings -> Project: -> Project Structure

Then mark projects as a source directory.

like image 191
Spike Curtis Avatar answered Oct 05 '22 05:10

Spike Curtis