ModuleNotFoundError: No module named - when attempting to import files from another folder

ModuleNotFoundError: No module named - when attempting to import files from another folder

我正在尝试 运行 从另一个文件夹导入 .py 文件的代码。层级如下:

这是我的 AppiumTest 文件中调用导入的代码部分:

from appium import webdriver
import unittest
from test.pageObj.LoginPage import LoginActivity
from test.pageObj.HomePage import HomeActivity

尝试 运行 时收到此错误消息:

ModuleNotFoundError: No module named 'test.pageObj'

我错过了什么?

尝试:

from pageObj.LoginPage import LoginActivity
from pageObj.HomePage import HomeActivity

你能试试下面的代码,看看它是否有效吗?

从 pageObj.LoginPage 导入 LoginActivity

从 pageObj.HomePage 导入 HomeActivity

  1. __ 在 pageObj
  2. 中初始化 __.py
from .HomePage import HomeActivity
from .LoginPage import LoginActivity
  1. AppiumTest.py
from test.pageObj import HomePage
from test.pageObj import LoginPage
  1. 包和 __ 初始化 __

Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an init.py file. When a regular package is imported, this init.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The init.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.

您不需要在导入语句中包括父目录。 from pageobj import LoginActivity 应该可以。