10

I have several Python projects and they all have a conf package:

/some_folder/project_1/
  conf/
    __init__.py
    some_source_file.py

/another_folder/project_2/
  conf/
    __init__.py
    another_source_file.py

For each project, I have created a .pth file in the site-packages folder with this contents:

.../site-packages/project_1.pth:
import sys; sys.path.append('/some_folder/project_1/')

.../site-packages/project_2.pth:
import sys; sys.path.append('/another_folder/project_2/')

I can access modules in /some_folder/project_1/:

import conf.some_source_file

but not the modules in /another_folder/project_2/:

import conf.another_source_file
AttributeError: 'module' object has no attribute 'another_source_file'

It looks as if python only searches the first conf path underneath any folders in sys.path. Is there a way to fix that?

informatik01
  • 16,038
  • 10
  • 74
  • 104
ssc
  • 9,528
  • 10
  • 64
  • 94
  • Possible duplicate of [Python : importing different module with same name](http://stackoverflow.com/questions/32884206/python-importing-different-module-with-same-name) – luator Nov 07 '16 at 08:10

1 Answers1

9

No. You will need to either rename one of them or turn the project directory into a package and import via that.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 1
    correct. i've actually been doing that all along and now I realize the problem does not seem to be with python itself, but with the mod_wsgi apache module in whose context the second project is running. that's a whole different story of course, so I've posted a new question: http://stackoverflow.com/questions/6631826/mod-wsgi-import-python-modules-with-the-same-name – ssc Jul 09 '11 at 00:13
  • Actually this is solvable using [python - How can I import a module dynamically given the full path? - Stack Overflow](https://stackoverflow.com/questions/67631/how-can-i-import-a-module-dynamically-given-the-full-path) – user202729 Feb 27 '23 at 01:00