PyCharm lets you safely restructure your source files by converting a Python module to a Python package and vise versa.
Convert to a Python package
A package typically is a directory that contains modules and initialization code (file
__init__.py). A module is a
.py source file with Python definitions that can be imported to other modules.
To convert a Python module to a Python package
- Select a
- Select .
- Inspect the project: the package named as the converted module is created; the
__init__.pyfile contains all code from the
With the package created, you can add more new modules to it, or you can use Move and Copy refactorings to derive modules from the initial implementation. You can also modify the
__init__.py file to put some initialization code for the package or list all the added modules by using the
Convert to a Python module
You might need to consolidate all modules from a package into a single module.
To convert a Python package to a Python module
- Prepare your package for refactoring: move code from all modules in the package to the
__init__.pyfile. See Move and Copy refactorings for more details. This is the key action, because only an empty package (a package that has no files but the
__init__.pyfile) can be converted into a module.
- Select the package.
- Select .
- Inspect the project: the
.pyfile named as the converted package is created; it contains code from the