2

Hey I have a tree structure with an empty init.py in each directory/subdirectory. However, I get a failed to load module Utilities from my Home directory .py file.

Using python 3.7

My tree looks like the following:

 C:. ├───Tests │ ├───Checkout │ ├───GlobalFooter │ ├───GlobalHeader │ │ └───__pycache__ │ ├───Home │ │ └───__pycache__ │ ├───MyAccount │ ├───ProductDetail │ ├───ProductResults │ │ └───__pycache__ │ └───SignIn └───Utilities └───__pycache__ 

I have tried the following:

sys.path.insert(0, 'C:/Web2/TSC.WebFactory.Web2.Tests/Utilities') from Utilities.utils import addCookies, configureOptions 

2 Answers 2

3

If you want to access your whole tree of modules by their package qualified names, you shouldn't include Utilities in the sys.path entry; from Utilities.utils import ... assumes that some folder in sys.path has a package/folder named Utilities, that contains either a subpackage/folder named utils or a submodule/file named utils.py; by adding 'C:/Web2/TSC.WebFactory.Web2.Tests/Utilities' to sys.path, it expects 'C:/Web2/TSC.WebFactory.Web2.Tests/Utilities/Utilities/utils.py' (note doubled Utilities).

The solution here is to remove that final directory from the path:

sys.path.insert(0, 'C:/Web2/TSC.WebFactory.Web2.Tests') 

Now from Utilities.utils import ... will look in C:\Web2\TSC.WebFactory.Web2.Tests for Utilities\utils.py and find it as expected.

Side-note: If you want to keep to Windows standard backslashes as directory separators, you can use them fairly cleanly by just making the path a raw string literal, avoiding the need for constant escaping:

sys.path.insert(0, r'C:\Web2\TSC.WebFactory.Web2.Tests') 
Sign up to request clarification or add additional context in comments.

Comments

0

If you haven't already tried, do this as when doing sys.path.insert you shouldn't have to specify the folder on the import line.

from utils import addCookies, configureOptions

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.