Arshanvit
Arshanvit

Reputation: 467

Include both type of imports in python package

I am new to python packages and currently using tox with pyscaffold to prepare packages.

The structure of my code directory is as follows:

-- project
-- src
----__init__.py
---- data_prep.py
---- modelling.py
---- main.py

Now when i build package with tox -e build, i need to call the modelling.py inside main.py with following signature

from . import modelling

But when i am trying to execute it standalone by just introducing main in main.py class in bottom.That time, this kind of import not works and i have to change above import to

import modelling

How can I modify this import statement so that it suffices both cases.Kindly help.

init.py

import sys

if sys.version_info[:2] >= (3, 8):
    # TODO: Import directly (no need for conditional) when `python_requires = >= 3.8`
    from importlib.metadata import PackageNotFoundError, version  # pragma: no cover
else:
    from importlib_metadata import PackageNotFoundError, version  # pragma: no cover

try:
    # Change here if project is renamed and does not equal the package name
    dist_name = "fqr"
    __version__ = version(dist_name)
except PackageNotFoundError:  # pragma: no cover
    __version__ = "unknown"
finally:
    del version, PackageNotFoundError

main.py(for standalone purposes)

def main():
   ##relevant code lines

if __name__=="__main__":
  main()

Upvotes: 0

Views: 138

Answers (0)

Related Questions