My project is a tool for people interested in role playing games. My
current structure looks something like this:
/src
rpgDirectory.py (main script, running the app)
src/rpg
plans.py
support.py
gui.py
iosystem.py
src/rpg/character
model.py
sheet.py
gui.py
handlers.py
requirements.py
The problem is, that modules from src/rpg/character use classes
defined in support.py. Therefore I have to use absolute paths to
import it and this works only, when I run rpgDirectory.py. When I use
pychecker, it can't import this module and fails. Any suggestions, how
can I avoid this and what structure should I use?
--
Filip Gruszczyński
That's exactly what I have read before posting here ;-)
--
Filip Gruszczyński
I too would like to see a meatier discussion of best practices
for python packagizing. I particularly object to jcalderone's
suggestion that all tests should be within the package. Often my
test cases are 100 times the size of the code, if you include
sample data -- it doesn't make sense to install it, I think.
Lately I tend to have something like
root/
setup.py
README.txt
packagename/
main source modules
submodules...
test/
test code and submodules
doc/
scripts/
testdata/
demos/
etcetera... I have no real idea if this is a good
way to do it, but it works for me in isolation.
I'm completely confused about any implications related to
integrated system testing or "easyinstall"...
Wise, pragmatic advice would be appreciated. (But if we
could avoid the "buzzillion directories" approach prevalent
in the java alternative universe, that would be nice.)
-- Aaron Watters
===
Now we see the violence inherent in the system!
Help! Help! I'm being repressed!
When I am doing an application type of code for myself, I usually end up
organizing my program the way it is the easiest to maintain, if
necessary in this case I would sacrifice small and simple over
robustness and maintainability.
When I am starting a project which is either a payed contract job or I
anticipate other participants, I orientate my project more around
documentation and management, I have written an article in the past
about that (generally written it was still more aimed to the client I
was working at that moment). If anybody is interested you can get it
here:
http://dcuktec.googlecode.com/svn/trunk/documentation/best_practises/DITDD/deliverables/DITDD.pdf
ymmv
Hi,
I have read in many places that relative imports aren't recommend as a
standard. This includes PEP 8 (http://www.python.org/dev/peps/
pep-0008/) which states:
" - Relative imports for intra-package imports are highly discouraged.
Always use the absolute package path for all imports.
Even now that PEP 328 [7] is fully implemented in Python 2.5,
its style of explicit relative imports is actively discouraged;
absolute imports are more portable and usually more readable."
...and I completely agree. I always use the standard import form
unless absolutely necessary. However, I use 'as' to shorten the path
to the last module. For example:
>>> import app.foo.bar as bar
>>> instance = bar.Class()
The only "relative" import I use when I am getting another module in
the same package. If I have:
app/
__init__.py
constants.py
foo/
__init__.py
bar.py
here.py
utils/
__init__.py
... and I am inside app/foo/here.py, I might have some imports at the
top of the module which look like this...
import app.constants as appC
import app.utils
import bar
Python will look for 'bar' in the local package before looking through
the python path. I could have imported constants as just "c", but
single letter variables are dangerous and I work with an application
where it is common in the community to use 'c' for
'constants' (regardless of the danger). Lastly, I could import
'app.utils' as 'utils', but this is such a common module name that I
like to preserve the name-space or at least prefix it (so I suppose
something like 'apputils' would be acceptable, but I'd only be saving
one character, the '.', so what's the point?).
I find that no matter how well I plan what my structure will be, I end
up making small changes such as flattening a sub-package or converting
a module to a sub-package to break things down further. As someone who
recently started learning python, I would recommend that you just make
a quick sketch of what you think might work and then just begin
coding. Adjust to logic along the way.
At some point planning begins to eat time rather than save it. get
through the fastest initial 80%, maybe push for a few more %, then
just go for it (I assume you have this luxury. If not then you
probably have a team that can help refine the plan anyway.)
Hope this helps.
- Rafe
Why bother with ``as`` in this case?
from app.foo import bar
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan