How to import a package that you have written in PyCharm?
To import a package that you have written yourself, you need to follow these steps:
- Create a folder as the root directory of the package, and within the folder, create an empty __init__.py file to identify the folder as a package.
- Create your own module file in the root directory and write code in it. For example, create a file named module.py and define some functions or classes in it.
- In the place where the package is needed, you can import it using the import statement. For example, if they are in the same directory, you can import the module using the following code.
import module
- A component
module.my_function()
Note: if the package’s root directory is not in the current directory, you can use relative or absolute imports. A relative import uses “.” to represent the current directory and “..” to represent the parent directory, for example, “from . import module”. An absolute import uses the full package name, for example, “from mypackage import module”.
PyCharm also offers intelligent suggestions and automatic import features, making it more convenient to import your own packages. You can use Ctrl+Space to view available import options and select the appropriate package or module for importing.