tags:

views:

36

answers:

3

Hi,

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 !

A: 

You can tweak your PYTHONPATH environment variable or edit sys.path.

Nathon
A: 

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.

GreenMatt
That did the trick, thanks !
chouquette
If you're using relative imports, you could also do `import os; os.chdir('PATH_TO/config')` if you felt like it.
JAB
A: 

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.

Pinballkid