Import from subfolder hierarchy in python

I am trying to import the specified modules from the test_file hierarchy

sort of:

test_case1.py test_subsuite_2 test_sub_2.1.1.py test_suite2 

is it possible to import the execution of this hierarchy

 /project/main.py /project/test_files 

The hierarchy of the test_files folders is as follows:

 test_files test_suite1 test_case1.py test_subsuite_1 test_sub1_1.py test_sub1_2.py test_subsuite_2 test_subsuite_2_1 test_sub_2.1.1.py test_sub2_1.py test_sub3_2.py test_suite2 test_case2.py test_subsuite2_1 test_sub21_1.py test_sub21_2.py test_subsuite2_2 test_sub22_1.py test_sub23_2.py 
+4
source share
3 answers

Create an empty file named __init__.py in all of your folders. Then you can import using . as a folder separator. The documentation is here.

+5
source

The key is to create an empty __init__.py file in all subfolders containing the files you want to import. In your case, you will need to create __init__.py files for all of the following folders-

  • test_files
  • test_files \ test_suite1
  • test_files \ test_suite1 \ test_subsuite_2
  • test_files \ test_suite1 \ test_subsuite_2 \ test_subsuite_2_1

In addition, when importing files, be careful with the correct import path, indicating the entire path from the highest level and with different levels of folders separated .
For example, you should import test_case1, specifying:

from test_files.test_suite1 import test_case1

Similarly, test_subsuite_2 can be imported by specifying:

from test_files.test_suite1 import test_subsuite_2

+2
source

This still does not work for some strange reason. Here is my tree structure:

 TestProject/ ├── __init__.py ├── Locators │  ├── __init__.py │  └── locators.py <---- Locators location ├── POM │  ├── homePage.py │  ├── homePage_wl.py <----- importing from Locators │  ├── __init__.py │  ├── loginPage.py │  ├── loginPage_wl.py <----- importing from Locators │  └── __pycache__ │  ├── homePage.cpython-37.pyc │  ├── __init__.cpython-37.pyc │  ├── loginPage.cpython-37.pyc │  └── loginPage_wl.cpython-37.pyc ├── __pycache__ │  ├── login.cpython-37.pyc │  ├── login_pom.cpython-37.pyc │  ├── test_login.cpython-37.pyc │  ├── test_login_pom.cpython-37.pyc │  └── test_login_pom_wl.cpython-37.pyc ├── test_login_pom.py ├── test_login_pom_wl.py ├── test_login.py └── tree.txt 

From the file /POM/loginPage_wl.py I import like this:

 from TestProject.Locators.locators import Locators 

And I see the same error:

 ImportError: Failed to import test module: test_login_pom_wl Traceback (most recent call last): File "/media/Work/anaconda3/lib/python3.7/unittest/loader.py", line 154, in loadTestsFromName module = __import__(module_name) File "/media/Work/Work/selenium/TestProject/test_login_pom_wl.py", line 5, in <module> from POM.loginPage_wl import LoginPage File "/media/Work/Work/selenium/TestProject/POM/loginPage_wl.py", line 1, in <module> from TestProject.Locators.locators import Locators ModuleNotFoundError: No module named 'TestProject' 

I looked all over the network and not a single soul worked. Any suggestions?

0
source

All Articles