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:
Things I've observed:
And the questions:
Thanks in advance.
First off, you will find all the information you need in section 6 of The Python Tutorial.
(1) 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?
No, it doesn't. Actually, Python always searches sys.path
when importing modules. Modules in the current directory are only found since sys.path
contains an entry with the empty string, meaning the current directory.
(2) Why doesn't
import testpackage
give me access totestpackage.testmod
? When I importos
, I can then accessos.path
(etc).
For efficiency, import testpackage
only loads testpackage/__init__.py
. If you need testpackage.testmod
, you have to explicitely import it:
import testpackage # Just imports testpackage, not testpackage.testmod!
import testpackage.testmod # Import *both* testpackage and testpackage.testmod!
If you always want to export testmod
, import it within __init__.py
, this is what os
(os/__init__.py
) does. This way, testpackage.testmod
is always available implicitely if you import testpackage
.
Since Python is cross-platform, there is actually no way to consistently and automatically load modules in a directory, because some filesystems are case-insensitive (Windows!). Python would not know whether to load os/path.py
as os.path
or os.Path
, etc.
(3) 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?
You always need an __init__.py
for each subpackage. There were discussions on dropping this requirement, but it was decided to keep it as it is.
(4) How can I import a module specifying the package name? I.E. from
testmod.py
, I would like to importtestpackage.testmod2
rather than justtestmod2
.
This should work. Just ensure that you run the code from the top-level directory. If the current directory is testpackage
, testmod
does not know that it's in a package.
The preferred way is to use relative intra-package imports, though:
from . import testmod2
This prevents name clashes if there is a global module named testmod2
and enables you to use the names of well-known modules within your package without problems.
(5) 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.
No, don't do that! Never, ever put a directory to sys.path
when one of it's parent directories already is in sys.path
! This could cause your modules to be loaded twice, which is a bad thing!
Usually you should be able to load modules from subpackages using absolute or relative imports:
import testpackage.testsubs.testsubmod
from testpackage.testsubs import testsubmod
from .testsubs import testsubmod
Just make sure to create a __init__.py
within testsubs/
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With