Hi, Ok, so I should be using absolute imports, not relative imports.
Hmm, I just tried to use absolute imports, and it can't seem to locate the modules: In the file "foo_loading/em_load/sync_em.py", I have: from common.common_bex import setup_foo_logging When I try to run that script: python sync_em.py I get: ImportError: No module named common.common_foo I've also tried adding "foo_loading" (the package name): from foo_loading.common.common_bex import setup_foo_logging Same error: ImportError: No module named foo_loading.common.bex_common Any thoughts? Cheers, Victor On Tuesday, 29 October 2013 00:12:58 UTC+11, Jean-Michel Pichavant wrote: > ----- Original Message ----- > > Hi, > > > > I have a collection of Python scripts I'm using to load various bits > > of data into a database. > > > > I'd like to move some of the common functions (e.g. to setup loggers, > > reading in configuration etc.) into a common file, and import them > > from there. > > > > I've created empty __init__.py files, and my current directory > > structure looks something like this: > > > > foo_loading/ > > __init__.py > > common/ > > common_foo.py > > em_load/ > > __init__.py > > config.yaml > > sync_em.py > > pg_load/ > > __init__.py > > config.yaml > > sync_pg.py > > > > So from within the sync_em.py script, I'm trying to import a function > > from foo_loading/common/common_foo.py. > > > > from ..common.common_foo import setup_foo_logging > > > > I get the error: > > > > ValueError: Attempted relative import in non-package > > > > If I change directories to the parent of "foo_loading", then run > > > > python -m foo_loading.em_load.sync_em sync_em.py > > > > it works. However, this seems a bit roundabout, and I suspect I'm not > > doing things correctly. > > > > Ideally, I want a user to be able to just run sync_em.py from it's > > own directory, and have it correctly import the logging/config > > modules from common_foo.py, and just work. > > > > What is the correct way to achieve this? > > > > Secondly, if I want to move all of the config.yaml files to a common > > foo_loading/config.yaml, or even foo_loading/config/config.yaml, > > what is the correct way to access this from within the scripts? > > Should I just be using "../", or is there a better way? > > > > Cheers, > > Victor > > Long story short : use absolute imports. > > name properly your module with a distinct name and import that way, even > inside your package: > > import foo_loading.common.common_foo > > Names like common, lib, setup are farely prone to collision with other badly > referenced import from other modules. One way to solve this is to use a > distinct namespace, in other words, prefix every import with the module name. > > cheers, > > JM > > > -- IMPORTANT NOTICE: > > The contents of this email and any attachments are confidential and may also > be privileged. If you are not the intended recipient, please notify the > sender immediately and do not disclose the contents to any other person, use > it for any purpose, or store or copy the information in any medium. Thank you. -- https://mail.python.org/mailman/listinfo/python-list