Question

I'm not a newby in Python but I'm stucked with a strange issue.

I set this python project structure (excerpt):

  • project-A/myapp
  • project-B/core/common/lib

where myapp / core, common & lib are python packages.

project-B directory is set in the python path as '/root_path/to/core_module'

when I run the script that imports modules into the project-A/myapp directory :

from core.common.lib import Error, XmlSource, Property, Preference

it raises an error :

ImportError: No module named 'core'

without changing the current directory in python3 interpreter and typing in the same command :

jeby6372@mercure:~/factory/bubble$ python3
Python 3.4.0 (default, Apr  9 2014, 20:03:10) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from core.common.lib import Error, XmlSource, Property, Preference
>>> 

The module is loaded... I was expecting the same behaviour as above during the script execution.

so I try added this command before the import statement that fails

os.environ['PYTHONPATH'] = '/root_path/to/core_module'

with no success ...

Any idea ?

Perhaps am I wrong but I assume that the python3 interpreter searches the relative import modules path given first in the current directory and then in the defined python path wherever it's called.

Thanks for your help

Was it helpful?

Solution

Setting the PYTHONPATH inside the interpreter won't help since it' s read on interpreter startup. The solution would be to append the path to core module to sys.path directly before importing it, i.e.

import sys
sys.path.append('/path/to/core/module')
import core.module

I guess you forgot to export the pythonpath, try (in shell):

export PYTHONPATH='/path/to/core/module'
python your_python_script.py
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top