Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm not recognizing packages even when __init__.py exits

Tags:

python

pycharm

This is my directory structure

--> ProjectDirectory
          -->__init__.py


          --> BaseDirectory
                  -->__init__.py


          --> AnotherBaseDirectory
                  -->__init__.py

          -->program.py

inside program.py

When i give import BaseDirectory

PyCharm is unable to recognize the package

all __init__.py files contain __all__ variable with the python file names

What am i doing wrong?

like image 225
wolfgang Avatar asked Jul 15 '15 14:07

wolfgang


People also ask

Why is PyCharm not recognizing import?

Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.

Do you still need __ init __ py?

If you have setup.py in your project and you use find_packages() within it, it is necessary to have an __init__.py file in every directory for packages to be automatically found.

Why do you need a __ init __ py?

Without an __init__.py file, you cannot import files from another directory in a Python project.


3 Answers

You can try marking root directory as Sources Root. Right click on root directory, and click Mark Directory As -> Sources Root

like image 55
del-boy Avatar answered Oct 14 '22 19:10

del-boy


If we set __init__.py as a Text file type, Pycharm was not indexing it. We need to remove __init__.py from the Text file type enter image description here

like image 35
Song Avatar answered Oct 14 '22 18:10

Song


My problem was that I had a hyphen in the package name foo-bar, which also brings other problems with it. Replacing with an underscore: foo_bar immediately solved the problem.

like image 27
TheNickqq Avatar answered Oct 14 '22 19:10

TheNickqq