1

I'm working on the development of a some small Python packages in parallel, and I have organized my repository as follow:

|- root_folder
    |- source_code_folder
        |- package1
        |- package2
        |-...
    |- test_folder
        |- package1_test_folder
            |- package1_test.py

Where root_folder is the repository, source_code_folder contains the packages to be imported and tested in the test.py files contained in test_folder/packageX_test_folder.

I've written the following lines of code to add to the PATH the source_code_folder(not permanently) from whatever test_folder/package1_test_folder/test.py file.

if __name__ == '__main__':
    def __import_pkgs():
        # automatically add to the path the source_code_folder folder if it is not contained yet
        THIS_PATH = os.getcwd().split('\\')
        for i in range(THIS_PATH.index('root_folder'),len(THIS_PATH)-1): THIS_PATH.pop()
        ROOT_PATH = '\\'.join(THIS_PATH)
        PKG_PATH = ROOT_PATH + '\\source_code_folder\\'
        if sys.path.count(PKG_PATH) == 0: sys.path.append(PKG_PATH)
        return None

    __import_pkgs()

In this way, however, this __import_pkgs() function works only for Windows-based environments. Is there any way to made this independant to the OS systems? Anyone have any suggestion to make it more efficient/elegant?

andrew-94
  • 27
  • 4
  • `os.path` or `pathlib` should be used instead. Don't do stuff like you are currently doing. It will only introduce subtle bugs. – MegaIng Oct 10 '20 at 11:32
  • Do you mean that `os.path` methods should be used to handle the whole? I'm actually not a python expert, so every suggestion is more than welcome. – andrew-94 Oct 10 '20 at 11:44
  • Yes! Unless you have a really good reason, use `pathlib`. It handles everything for you. – MegaIng Oct 10 '20 at 11:50
  • Ok, thank you very much for the suggestions. I'll try to do so. – andrew-94 Oct 10 '20 at 11:53

2 Answers2

1

i recommend using "os.path" like so:

import sys
from os import path

if __name__ == '__main__':
    software_code_full_path = path.abspath(path.join(path.dirname(__file__), '../../source_code_folder'))

    if software_code_full_path not in sys.path:
        sys.path.append(software_code_full_path)
Mahdi Sadeghi
  • 673
  • 5
  • 13
0

use os.sep to get the path separator instead of explicitly using forward or backward slash

patel deven
  • 630
  • 7
  • 9
  • Thank you very much, this is useful. With that and the `os.path.join` method I've solved the problem. – andrew-94 Oct 10 '20 at 11:36
  • yup. ```os.path.join``` should always be . However you have to be very careful with ```os.path.join```. if any paths has a leading slash , results can be unexpected. For eg ```os.path.join('/a/b', '/c')```, might not yield the results you are expecting. – patel deven Oct 10 '20 at 11:48
  • Yeah, I see. Then, is a bit more complex than I expected. I'll go carefully through the documentation of `os.path` and see. The repository that you see on the post is not public, so I shouldn't have troubles using it with my pc (since I'm the only contributor), but its much better to handle that in a formal way so that I would have to share it, there will be no troubles. Thank you for the suggestions. – andrew-94 Oct 10 '20 at 11:57