- 将文件base_page.py当作脚本运行
sys_path、__name__和__package__如下:
['E:\\py_test\\unittest_demo\\pom_demo\\base_page', 'E:\\py_test\\unittest_demo', 'D:\\Python38\\python38.zip', 'D:\\Python38\\DLLs', 'D:\\Python38\\lib', 'D:\\Python38', 'D:\\Python38\\lib\\site-packages', 'D:\\Python38\\lib\\site-packages\\pip-20.2.4-py3.8.egg', 'D:\\Python38\\lib\\site-packages\\selenium-3.141.0-py3.8.egg']
__main__
None
- 将文件base_page.py当作模块导入
E:\py_test\unittest_demo\pom_demo\page_object\login_page.py的sys_path、__name__和__package__如下:
['E:\\py_test\\unittest_demo\\pom_demo\\page_object', 'E:\\py_test\\unittest_demo', 'D:\\Python38\\python38.zip', 'D:\\Python38\\DLLs', 'D:\\Python38\\lib', 'D:\\Python38', 'D:\\Python38\\lib\\site-packages', 'D:\\Python38\\lib\\site-packages\\pip-20.2.4-py3.8.egg', 'D:\\Python38\\lib\\site-packages\\selenium-3.141.0-py3.8.egg']
__main__
None
- E:\py_test\unittest_demo\pom_demo\base_page\base_page.py的sys_path、__name__和__package__如下:
['E:\\py_test\\unittest_demo\\pom_demo\\page_object', 'E:\\py_test\\unittest_demo', 'D:\\Python38\\python38.zip', 'D:\\Python38\\DLLs', 'D:\\Python38\\lib', 'D:\\Python38', 'D:\\Python38\\lib\\site-packages', 'D:\\Python38\\lib\\site-packages\\pip-20.2.4-py3.8.egg', 'D:\\Python38\\lib\\site-packages\\selenium-3.141.0-py3.8.egg']
pom_demo.base_page.base_page
pom_demo.base_page
- 原因
(1)程序入口默认被当作顶层、没有父目录,且package为空None,因此import要写绝对路径,from pom_demo.base_page.base_page import BasePage
。
否则报错attempted relative import with no known parent package。
(2)文件base_page.py的__packege__有内容,则表示可以在base_page.py的import中使用相对路径父目录..
(3)‘E:\py_test\unittest_demo’是pycharm new 的project,看上去能识别出project层次