Why sys.path.insert not using the latest python package?

List below is my project structure.

├── folder1
    ├── test.py
├── folder2
    ├── A.py
├── folder3
    ├── A.py

A.py in folder2 and folder3 are the same except self.key

#folder2
class TestAPI():
    def __init__(self):
        self.key = 50 


#folder3
class TestAPI():
    def __init__(self):
        self.key = 100  

In test.py, the code is

import sys
root_path = '/user/my_account'

sys.path.insert(0, root_path + '/folder2')
from A import TestAPI
test_api =  TestAPI()
print(test_api.key)

sys.path.insert(0, root_path + '/folder3')
from A import TestAPI
test_api = TestAPI()
print(test_api.key)
print(sys.path)

While executing test.py, it returns 50,50, ['/user/my_account/folder3', '/user/my_account/folder2']. Why the second time from A import TestAPI not from folder3 but folder2?



Read more here: https://stackoverflow.com/questions/66279033/why-sys-path-insert-not-using-the-latest-python-package

Content Attribution

This content was originally published by jasonshu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: