python - System Paths and Modules -


i have following setup:

/project/     /api/         __init__.py         test.py     /modules/         __init__.py         api.py 

i trying to, /project/ directory, run api.py: python modules/api.py

the api module attempts import test module api package, fails. have tried following:

import api.test import project.api.test  # (with __init__.py in /project/ directory) 

i have attempted add api package's parent directory system path described:

import sys import os sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 

question

how can set project in such way each package has knowledge of other packages in it's parent directory, project's root directory?

your problem have project/modules/api.py file projects/api directory __init__.py file in - you're getting descriptor collisions. rename 1 of them else, , code should work:

/project/     /api/         __init__.py         test.py     /modules/         __init__.py         foo.py 

then run python modules/foo.py , in foo.py:

from ..api import test 

or alternatively:

import sys import os sys.path.append(os.path.abspath('../api')) import test 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -