Let me start by saying I've done extensive research over the course of the past week and have not yet found actual answers to these questions - just some fuzzy answers that don't really explain what is going on. If that's just cause I missed what I was looking for, I'm sorry - please just point me in the correct direction.
My directory structure is:
TestProject/
runtest*
testpackage/
__init__.py
testmod.py
testmod2.py
testsubs/
testsubmod.py
A couple notes:
- I'm using python2.7 on Ubuntu
- I'm testing with bpython
- I'm running bpython from specific directories to test the way import behaves
- I'm attempting to follow best practices.
- This package is not installed, it's in a random dev directory
- This directory is not in the pythonpath
- I have a single init.py in the package directory
- There are no init.py files in the nested directories
- The init.py file is empty
- testpackage/testmod.py contains TestModClass
- testpackage/testsubs/testsubmod.py contains TestSubModClass
Things I've observed:
- When I run bpython from TestProject/ import testpackage works
- This does not import testpackage.testmod
- I cannot access testpackage.testmod at all
- When I run bpython from TestProject/ import testpackage.testmod fails
- When I run bpython from TestProject/ from testpackage import testmod works
- I can add code to init.py to explicitly import testmod.py, but not testsubs/testmod.py
- I don't think this is the correct way to do this, what if the user doesn't want to import that module?
- from testmod.py i can import testmod2, but not testpackage.testmod2
- This would be nice to do so that I can name my own modules with overlapping names with STL or twisted (such as testpackage.logging) without causing errors (it sucks to have to name my own modules crap like customerlogging, instead of just mypackage.logging)
And the questions:
- Does python deal differently with imports on packages & modules that exist in the pythonpath than when you are trying to import from your current directory?
- Why doesn't import testpackage give me access to testpackage.testmod? When I import os, I can then access os.path (etc).
- With a package, should I stick to using a single init.py in the base directory, or should I be nesting them in subsequent directories?
- How can I import a module specifying the package name? I.E. from testmod.py, I would like to import testpackage.testmod2 rather than just testmod2.
- What is the proper way to import submodules from the subsubs directory?
- The only solution I see is to append that directory to the pythonpath from init.py, but I don't know if that's the correct way.
Thanks in advance.