python importing file on virtual environment -


i writing web app using python3, venv , c9.io paas. have following structure of code:

batch_runner.py logic/     __init__.py     parsers/         __init__.py         time_parser.py         abstract_parser.py 

here batch_runner imports abstract_parser, which, in turn, import time_parser. installed , runs venv activated.

to specific, batch_runner.py contains:

from logic.parsers import abstract sys import argv  url = argv[1] = abstract(url) 

logic/__init__.py empty. logic/parsers/__init__.py contains:

from abstract_parser import abstract time_parser import _timeinfo 

if go logic , run python abstract_parser.py directly, works expected. however, if go 1 level up, , run python batch_runner.py, able import abstract_parser, can't find time_parser called abstract_parser, throwing importerror: no module named 'abstract'

do read importing python documentation on modules.

in case, 1 possible solution use relative imports inside package:

that is, in logic/parsers/__init__.py, use:

from .abstract_parser import abstract .time_parser import _timeinfo 

and in abstract_parser.py:

from .time_parser import _timeinfo 

this should let parsers/__init__.py find abstract_parser module , time_parser module.

the python import system has surprising number of traps can fall into. this blog post nick coghlan describes many of them, , consider must-read if you're planning develop package.


Comments