6

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?

wolfgang
  • 7,281
  • 12
  • 44
  • 72

8 Answers8

16

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

del-boy
  • 3,556
  • 3
  • 26
  • 39
8

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

Song
  • 593
  • 9
  • 21
  • 2
    Incredible, this was the issue for me, been trying to debug for a long time. The interpreter was able to read the __init__.py but Pycharm was not indexing it. Intellij please find the reason why this would default to text. – Jared Marks Nov 03 '20 at 17:43
  • Thank you! This issue has been annoying me like a gnat for months. – Darren H Mar 05 '21 at 20:35
  • @Song, to be clear, should the __init__.py be a Text file or something else? The message you typed was somewhat ambiguous. "Please have a check if ..." and do what as a consequence of checking? 1) please assure that it is a Text file 2) or please check whether it is, and change it to type XYZZY. – Jim Newton Jul 01 '21 at 09:54
  • In my case the __init__.py was somehow falsely added to another File Types recognition, therefore Pycharm will not try to read its reference as Python file type. Remove it and the editor will work correctly. – kbxu Oct 30 '21 at 09:49
  • appreciate man. accidentally added the init file to another file type!! – Diganta Protic Biswas Nov 18 '21 at 12:03
  • 1
    Thanks, this was it for me. In my case, it was under "File type autodetected by file content". Try typing it explicitly under the python file types and it will give you a warning if it is explicitly declared elsewhere. – natehawkboss Jan 12 '22 at 00:10
4

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.

TheNickqq
  • 63
  • 6
0

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:

xxks-kkk
  • 2,336
  • 3
  • 28
  • 48
0

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
iwbabn
  • 1,275
  • 4
  • 17
  • 32
0

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.

0

If the highlighting is not working for __init__.py, that means, that PyCharm thinks it is a different type, so the packages won't work.

The file name patterns can be found in the Editor > File Types section in the settings (File > Settings) __init__.py can be in categories, like Text, or File type auto-detected by file content. You don't need to search, just put __init__.py to the Python category, and everything will be all right again (after complaining about the init file is already being in other category).

Other problem can be if that directory that contains the init file has no proper name. If you stick with the PEP8 file name convention you must be safe, however these names are just a subset of acceptable file names.

Settings > Editor > File Types > Python

-1

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 +.

juanmajmjr
  • 1,055
  • 7
  • 11