python - Is it really possible to use a different convention to lay out source directory without breaking intra-package references? -
suppose working on python package. here structure of package (expressed in terms of hierarchical file system):
/packagename /src __init__.py /subpackage1 __init__.py module1a.py module1b.py /subpackage2 __init__.py module2a.py module2b.py /tests install license readme setup.py
distutils used build, distribute , install package. in setup script possible tell distutils convention lay out source directory (see here). in short put:
package_dir = {'packagename', 'src'}
however there issue when example module1a
needs import module2a
absolute import (see here). in short put in module1a
:
import src.module2a
once package distributed , installed pip
simple:
import packagename
raises:
importerror: no module names src.module2a
a simple solution rename src
directory packagename
(as question states) there solution use different convention lay out source directory (perhaps distutils
or pip
option missed).
thank in advance.
register package with:
package_dir = {'': 'src'}
and create packagename
directory in src
. want import packagename
, not src
. src
not meant package itself:
/packagename /src /packagename __init__.py /subpackage1 __init__.py module1a.py module1b.py /subpackage2 __init__.py module2a.py module2b.py /tests install license readme setup.py
you can use relative imports between modules anyway, absolute import now
from package.subpackage2 import module2b
or use relative imports:
from ..subpackage2 import module2b
sample real-life project: https://github.com/mjpieters/collective.transmogrifier
Comments
Post a Comment