user1234440
user1234440

Reputation: 23597

Python Packages and Modules

I've never really needed to use packages and modules before in python but now as my code base is getting bigger and bigger i'd like to structure it so its imported easier.

I've got 10+ .py files that are all part of a package. Instead of doing import each and every class when i need them, how can I just group them in the same name space so that I can also reference import package.componentA as x?

Right now when ever I utilize my all the code, I got to have the source files in the same directory. Is it also possible to package this in a central location so that I can have clean project code?

Thanks,

Upvotes: 1

Views: 60

Answers (2)

Jim Dennis
Jim Dennis

Reputation: 17520

Perhaps this guide may help:

Be Pythonic: __init__.py

Basically it describes how to use the __init__.py magic file to handled package imports.

Upvotes: 1

salparadise
salparadise

Reputation: 5815

Use the __init__.py file in the package and import the modules you want to use from there. So when you import the package it will import everything in that file.

Some good info on this method here.

Upvotes: 1

Related Questions