How to write a custom solution using a python package, modules etc
Posted
by morpheous
on Stack Overflow
See other posts from Stack Overflow
or by morpheous
Published on 2010-05-21T12:53:38Z
Indexed on
2010/05/21
13:10 UTC
Read the original article
Hit count: 346
python
I am writing a packacge foobar which consists of the modules alice, bob, charles and david.
From my understanding of Python packages and modules, this means I will create a folder foobar, with the following subdirectories and files (please correct if I am wrong)
foobar/
__init__.py
alice/alice.py
bob/bob.py
charles/charles.py
david/david.py
The package should be executable, so that in addition to making the modules alice, bob etc available as 'libraries', I should also be able to use foobar in a script like this:
python foobar --args=someargs
Question1:
Can a package be made executable and used in a script like I described above?
Question 2
The various modules will use code that I want to refactor into a common library. Does that mean creating a new sub directory 'foobar/common' and placing common.py in that folder?
Question 3
How will the modules foo import the common module ? Is it 'from foobar import common' or can I not use this since these modules are part of the package?
Question 4
I want to add logic for when the foobar package is being used in a script (assuming this can be done - I have only seen it done for modules)
The code used is something like:
if __name__ == "__main__":
dosomething()
where (in which file) would I put this logic ?
© Stack Overflow or respective owner