python - Import modules from parent folder with Flask -


my folder tree:

project/     app/         __init__.py         models.py         dir/test1.py         dir/__init__.py     run.py     dir/test2.py     dir/__init__.py 

if want a

from app.models import whatever 

from test1 , test2 thing works manually sys.path.append

os.path.join(os.path.dirname(__file__), "../..") 

however there ton of answers on saying messing sys.path give me troubles down line (why?); sadly, after 1+ hour of googling still haven't figured out right way import stuff , i'm getting confused.

it enormously better test not test, if need append paths sys.path make work--and in directory configuration, will--that's reasonable , pragmatic step.

however, in general better not fiddle module load paths manually. assumes code will loaded in directory right outside test folder, might not true. "you run problems down line" pretty weak tea.

the bigger issue cannot use little path-patch accomplish kind of automated testing , test management want/need. better learn use real test harness / test runner such pytest or nose. better if use macro test runner such tox. combination automatically install software (including declared dependencies) in new, pristine virtual environment. test not normal operability of module, installability. run tests across different versions of python little additional effort. (i test across range of 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, , several versions of pypy, example--not because use versions, because doesn't cost me make sure software runs across large swath of entire python universe.)

fair warning: setting testing environment bit of pill first time out, requiring fair amount of effort , learning of "mechanic" skills. one-time investment pay dividends across of python work.

so long story short, patching sys.path fair place start. you'll need more generality, breadth, depth, test fixtures, , automation in testing--and path patching can't those. upgrading real testing tools can.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -