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?
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With