python 3.x - Import package "x" in module "x": prevent importing itself and import package instead -


i run problems when using project structure suggested here: what best project structure python application?.

imagine project layout this:

project/ |-- bin/ |   |-- project.py | |-- project/ |   |-- __init__.py |   |-- foo.py 

in bin/project.py want import package project.

#project.py project import foo 

since sys.path[0] project/bin when running bin/project.py, tries import module bin/project.py (itself) resulting in attribute error. there way use project layout without playing around sys.path in module bin/project.py? need "importpackage" statement, ignores modules same name.

since project structure suggested, i'm wondering why no 1 else has kind of problems...

you try:

import imp module_name = imp.load_source('project','../project') 

module_name package.

edit:

for python 3.3+

import importlib.machinery  loader = importlib.machinery.sourcefileloader("project", "../project") foo = loader.load_module("project") 

source


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -