Skip to content Skip to sidebar Skip to footer

How Do I Avoid Users Of My Code Having To Type Redundant Import Lines?

So I have a project called 'Pants' that lives on GitHub.com. Originally the project was a single .py file called pants.py. Pants/ pants.py README.md and users could imp

Solution 1:

To fix it, I kept my package structure the same, and added the following lines to pants/__init__.py:

from .antimportAntfrom .worldimportWorldfrom .solverimportSolver

Then I was able to change the import lines at the top of my demo file to:

from pants importWorldfrom pants importSolver

Solution 2:

Instead of having a separate pants.py put that code inside of __init__.py. Then when someone does import pants it loads that __init__.py. (I'd avoid using the uppercase "Pants" as uppercase is generally for classnames)

If your users need world or solver separately you can also do

from pants import world,solverw= world.World()
s = solver.Solver()

If they wanted everything from your package they could do

from pants import *

Post a Comment for "How Do I Avoid Users Of My Code Having To Type Redundant Import Lines?"