4

I've tried to import a module for testing but received this error:

Traceback (most recent call last): File "BaseTest.py", line 8, in from .main.pageobjects.FBPage import * ModuleNotFoundError: No module named 'main.main'; 'main' is not a package

the project tree looks like this:

/- ProjectDir
/- .src
/- .src.main
/- .src.main.core
/- .src.main.core.BaseCode <- base code to be extended as parent
/- .src.main.core.pageobjects
/- .src.main.core.pageobjects.Module <- a module that inherit from BaseCode 
/- .src.tests
/- .src.tests.BaseTest <- main testing module
/- .src.tests.results

I basically did this inside my BaseTest module:

from .main.pageobjects.Module import *

What am I doing here wrong? :)

3
  • Could you please post what all other imports worked ? Commented Dec 24, 2017 at 15:13
  • What version of Python are you using? Commented Dec 24, 2017 at 15:15
  • Python 3.6.1 import os, import sys, import unittest, from selenium import webdriver, from .main.pageobjects.Module import * Commented Dec 24, 2017 at 15:24

2 Answers 2

4

You have to import specific class (or classes) under the Module. See the code snippet below.I have explained the logic below the code.

from src.main.core.pageobjects.Module.module1 import Module1
from src.main.core.pageobjects.Module.module2 import Module2

Inside the Module, I created two python files: module 1 and module 2; in module1, I created a class - Module1; in module2, I created a class - Module 2; and then I used the below code snippet to import the two classes in the package

src.tests.BaseTest

Click the link for the project structure screenshot. How to import a module from different directory with Python 3? See the python codes for module1, module2, and logintest under BaseTest Module.

module1.py

class Module1(object): def init(self): print('This is a module 1 example')

def print_info(self):
    print("this is module 1 print statement")

module2.py

class Module2(object): def init(self): print('This is a module 2 example')

def print_info(self):
    print("this is module 2 print statement")

logintest.py under src.tests.BaseTest module

    from src.main.core.pageobjects.Module.module1 import Module1
from src.main.core.pageobjects.Module.module2 import Module2

m1=Module1()
m2=Module2()
m1.print_info()
m2.print_info()

Run the logintest.py and see the result. The result is from module1 and module2 in the src.main.core.pageobjects.Module package.

C:\Python36\python.exe C:/Users/SeleniumMaster/PycharmProjects/ProjectDir/src/tests/BaseTest/logintest.py
This is a module 1 example
This is a module 2 example
this is module 1 print statement
this is module 2 print statement

Process finished with exit code 0
Sign up to request clarification or add additional context in comments.

2 Comments

I did the some thing as you described following your instructions and received the same error as before
Traceback (most recent call last): File "BaseTest.py", line 8, in <module> from src.main.core.Utils.setup import load ModuleNotFoundError: No module named 'src'
2

You have to create a file called: __init__.py in /- .src.main.core.pageobjects. then in BaseTest.py add:

import sys
import os
os.chdir('../main/core/pageobjects')
current_dir = os.getcwd()
sys.path.append(current_dir)
from Module import *
do_something

the code above adds this path (/- .src.main.core.pageobjects) to the PYTHONPATH environment variable. Python uses PYTHONPATH variable to search for the modules that are imported , so once you add the full path , you can access the Module everywhere.

8 Comments

The init.py file should contain something in it?
@SysMurff No , it should be an empty file
Just added init.py file in all the directories that contains modules, but still same result and not same error: Traceback (most recent call last): File "BaseTest.py", line 8, in <module> from ..core.pageobjects.Module import * ValueError: attempted relative import beyond top-level package
please let me see the Error message
Traceback (most recent call last): File "BaseTest.py", line 8, in <module> from ..core.pageobjects.Module import * ValueError: attempted relative import beyond top-level package
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.