无法在python模块上导入类。
这是我的目录结构:
TestMap
+lib
+vendors
+testing
- _init.py
- products.py
- _init_py.
- notifications.py
- scraper.py
- utils.py
-main.py
我从scraper.py
开始,并尝试在vendors -> testing -> products.py
的product.py上获取功能
from .vendors.testing.products import TestProducts
我想做的是:
ImportError: cannot import name 'TestProducts' from 'lib.vendors.testing.products' (C:\Users\Annoynmous\Desktop\TestMap\lib\vendors\testing\products.py)
并且在products.py中,类名是:
class TestProducts():
我无法掌握我实际上在做错什么吗?
答案 0 :(得分:1)
使用pythonpath
设置项目中的源目录:
export PATH=$PATH:/home/user/somepath/TestMap
并从该源路径导入模块
from lib.vendors.testing.products import TestProducts
或使用__init__.py
文件定义内部模块目录,并使用直接模块名称导入模块。
答案 1 :(得分:1)
在 scraper.py
中尝试一下from vendors.testing.products import TestProducts
或
import vendors.testing.products as product
class scrapper:
def __init__(self):
self.product = product.TestProducts()
使用self.product
访问类刮板中的TestProducts的任何功能。