Difference between 'Directory' and 'Python Package' in PyCharm

14,785

When to use Directory over Python Package?

You can use "Python Package" when you want to put some modules in there which should be importable. PyCharm will automatically create an __init__.py for the directory.

Why not create everything as a Python Package?

Not every subdirectory in a project should necessarily be a package. For example docs and tests are commonly just directories.

Does PyCharm mark a location as one or the other based on its name?

PyCharm seems to mark the icon with a dot if the subdirectory name is a valid identifier and not a keyword, regardless of whether the subdirectory is a package or not. This is possibly because, in Python 3.3+, subdirs are also implicit namespace packages (they are still importable even when there is no __init__.py file).

If you have a project associated with a Python 2.7 interpreter, you don't get the dot on the icon unless the __init__.py file is added, since implicit namespace packages are not a thing in Python 2.

Share:
14,785
GuSuku
Author by

GuSuku

Machine Learning, Data Science

Updated on June 08, 2022

Comments

  • GuSuku
    GuSuku about 2 years

    Denter image description here

    1. When to use Directory over Python Package? PS: I understand that I can import from latter but not former. If so, why not create everything as a Python Package?
    2. Also, does PyCharm mark a location as one or the other based on its name? What is the pattern behind this behavior? For example, I created a Directory and named it 'lambda'. But when I renamed it to 'lambdas', pycharm automatically changed it to a Python Package (the briefcase with dot symbol). Python keyword?
  • GuSuku
    GuSuku almost 5 years
    Thanks. To clarify, in Python 3.3+, PyCharm marks a subdir with a dot anyways (as long as it is an 'identifier'). Does that mean that every subdir is importable in this case? if so, how do I unmark that like for tests and docs, like you mentioned?
  • wim
    wim over 4 years
    Yes, it means every subdir is importable. I don't know how to unmark it in PyCharm, probably you can't.
  • alkanschtein
    alkanschtein over 3 years
    from services.StockAnalyzer import StockReport either the services has init.py or not I can import in this way can you explain what kind of constraints would I have if I have init file or not I do not understand the difference