'Pycharm not recognizing packages even when __init__.py exits

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?



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

For whoever struggles with this question, I find my solution on Pycharm 2018 as following:

The idea is to run our python code in -m way but instead of doing

python -m module.a.b arguments

we do

runpy.py module.a.b arguments

As per the runpy documentation, it is the script that

implements the -m command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.

Here are the steps we actually do:

  • Find the runpy.py located inside the system. For my case, I'm using centos and runpy.py is located under /usr/lib64/python3.6/runpy.py

  • Setup the Pycharm configuration like the following:

Pycharm configuration for running a subpackage script

In my case, my script that is part of module is (i.e. module.a.b) quickstart.cmd.genXML and the arguments to the script is -p ..

In this way, we can also invoke python debugger inside pycharm against the script as usual.

Reference:

Solution 5:[5]

The answers here don't work for me, but the following does (Pycharm community 2018.3):

  1. Detach the project from PyCharm
  2. Close PyCharm
  3. Delete .idea folder from project
  4. Launch PyCharm
  5. Reopen the project and attach to current window
  6. Suddenly the project is recognized as package

Solution 6:[6]

what finally fixed this for me as init.py was not associated as a Text file was going to Auto-detect file type by content which is up around the beginning of the Recognized Files Types list and removing init.py from the list.

Solution 7:[7]

You can try by adding the ProjectDirectory into the path. To do that you have to go to: Settings -> Project Settings -> Project Interpreter and there you can add into the path by pressing +.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 del-boy
Solution 2
Solution 3 TheNickqq
Solution 4 xxks-kkk
Solution 5 iwbabn
Solution 6 Winston Smith
Solution 7 juanmajmjr