0

I have two projects, each one with a Python folder where I have another folder named lib_py with some shared libraries Python files. Note that even lib_py is a set of shared libraries, each project can have older/newer version of lib_py files (basing on its needs). So that's why each project needs to have its lib_py folder. The hierarchy is like following:

Project1
  Python
    lib_py
      __init__.py
      example.py
    main
      main.py
Project2
  Python
    lib_py
      __init__.py
      example.py
    main
      main.py

In both main.py of two projects, example.py is imported as follow:

from lib_py.example import x

In order each main.py has access to example.py, I append two following paths in my sys.paths:

D:\Project1\Python
D:\Project2\Python

The problem happens here: main.py from Project1 is called, the good example.py is used but when right after calling main.py from Project1, main.py from Project2 is called, again example.py from Project1 is called although I expect that example.py from Project2 is used.

Is there any way to precise that which package should be used if several are found with same names? Is there any other solution in your opinion? (without changing directory hierarchies of-course)

Thanks

Sacha
  • 134
  • 2
  • 12

2 Answers2

2

Well you could do something like this

Referring this answer

from os import path
import sys
sys.path.append(path.abspath('../Foo'))
sys.path.append(path.abspath('../Foo2'))


from Project1.python.lib_py import example  as proj1ex
from Project2.python.lib_py import example  as proj2ex
Community
  • 1
  • 1
onkar
  • 4,427
  • 10
  • 52
  • 89
  • The same remark that I wrote for Arun G suggestion: The problem is the scheme I described here is very simple version of the reality. In fact the hierarchies in Project1 and Project2 is more complex (lot of folder levels) and there are lot of more folder inside each project. Projects are not only Python projects but C++. So if I use your suggested solution, firstly I should add lot of __ init __.py inside lot of folders and secondly the import will be more complex as there are lot of levels. – Sacha May 30 '16 at 10:18
  • Rather than using sys.path.append, you could also add parent directory of Project1 and Project2 to the PYTHONPATH – Overdrivr May 30 '16 at 11:32
1

Just give the path from project level and alias it with other name

from project1.python.lib_py import example as ex1
from project2.python.lib_py import example as ex2

and start using ex1 and ex2

Arun G
  • 1,678
  • 15
  • 17
  • The problem is the scheme I described here is very simple version of the reality. In fact the hierarchies in Project1 and Project2 is more complex (lot of folder levels) and there are lot of more folder inside each project. Projects are not only Python projects but C++. So if I use your suggested solution, firstly I should add lot of __ init __.py inside lot of folders and secondly the import will be more complex as there are lot of levels. – Sacha May 30 '16 at 10:15
  • unfortunately. you need to explicitly mention them. i mean all of them!! – Arun G May 30 '16 at 10:20