I have this kind of path architecture :
>main_path/
__init__.py
config/
__init__.py
common.py
app_1/
__init__.py
config.py
index.py
>
I'd like to be able to do so in config.py
:
>from main_path.config import common
>
Though it does not work. Python tells me :
> $> pwd
..../main_path/app_1
$> python index.py
[...]
ImportError: No module named main_path.config
>
As far as I understand, this would be possible if i loaded everything up from the main_path, though the aim is to have multiple apps with a common config file.
I tried to add the parent directory to the_开发者_运维问答_path__
in the app_1/__init__
.py but it changed nothing.
My next move would be to have a symbolic link, though I don't really like this "solution", so if you have any idea to help me out, this would be much appreciated !
Thanks in advance !
According to the Modules documentation a module has to be in your PYTHONPATH environment variable to be imported. You can modify this within your program with something like:
import sys
sys.path.append('PATH_TO/config')
import common
For more information, you may want to see Modifying Python's Search Path in Installing Python Modules.
If you want to modify python's search path without having to set PYTHONPATH each time, you can add a path configuration file (.pth file) to a directory that is already on the python path.
This document describes it in detail: http://docs.python.org/install/#inst-search-path
The most convenient way is to add a path configuration file to a directory that’s already on Python’s path, usually to the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path.
You can tweak your PYTHONPATH environment variable or edit sys.path.
精彩评论