简体   繁体   中英

Python Imports from common directories

We have multiple projects using common python code.

File Structure:

Parent_folder
--Project_folder
  --Project_A
    --(source code)
  --Project_B
    --(source code)
--Common_folder
  --Common_library_A
    --__init__.py
    --(source code)
  --Common_library_B
    --__init__.py
    --(source code)

For Project_A to import Common_library_A I can use:

 path = os.path.join(dirname(abspath(__file__)),"\\..\\..\\Common_folder\\")
 sys.path.append(path)
 import common_library_A

or I can look for 'parent_folder' on the current working directory, and use that as a base path to add common_folder to, which I then add to the sys path.

We can't use a fixed path, as different branches will need to use the version of the code in their branch.

Is there a 'cleaner' way of doing this?

I think it is ok. If you encounter problems you can change it.

Usually the common_folder is called site-packages. common_library_A and common_library_B would be installed separately into the site-packages folder.

But if you work in a version-control-system and everything in Parent_folder should be of the same version, then site-packages is no solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM