Skip to content Skip to sidebar Skip to footer

Error Converting Module To A Package

I have a module with many simple helper functions, but it is getting large and tedious to maintain. I'd like to break it into a simple package. My package directory is defined on

Solution 1:

You should also import a and b from the package in order for dir to list them. If you want to auto import modules in a package when importing a package, specify in __init__.py by adding import statements of the modules you want to import in __init__.py

test# package directory
├── module1.py
├── module2.py
├── __init__.py

To import module1 whenever you import the package, you __init__.py must contain the following.

import module1

You can import a module from the test package with from test import module2

Edit:

As long as different modules serve different purposes, its better to keep all the related helpers in their own module. All the modules that you want to import by default when your package is imported should be specified in the __init__.py file. Others can be imported whenever necessary. There shouldn't be any performance implications even if you import a module multiple times they are initialized only once as found here.

Solution 2:

I think the simplest answer is to import the modules you want like this:

import test.a
import test.b as b  # variant if you don't want the package name

Then you can call functions in the modules:

test.a.foo()
b.bar()

Post a Comment for "Error Converting Module To A Package"