[英]import error in custom python package
While reorganizing a scrapy project I want to create a package from my pipelines.py
file. 在重新组织一个草率的项目时,我想从我的pipelines.py
文件创建一个包。
I want to change this... 我想改变这个...
├── my_scraper
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines.py
│ ├── settings.py
│ └── spiders
└── scrapy.cfg
to this... 为此...
├── my_scraper
│ ├── __init__.py
│ ├── items.py
│ ├── pipelines
│ │ ├── __init__.py
│ │ ├── MyPipeline1.py
│ │ ├── MyPipeline2.py
│ │ └── MyPipeline3.py
│ ├── settings.py
│ └── spiders
└── scrapy.cfg
But after importing each pipeline class in pipelines/__init__.py
但是在导入管道pipelines/__init__.py
每个管道类pipelines/__init__.py
from my_pipeline1 import MyPipeline1
from my_pipeline2 import MyPipeline2
from my_pipeline3 import MyPipeline3
in and referencing each in settings.py
并在settings.py
引用每个
ITEM_PIPELINES = {
'my_scraper.pipelines.MyPipeline1': 100,
'my_scraper.pipelines.MyPipeline2': 200,
'my_scraper.pipelines.MyPipeline3': 300,
}
Now when I run scrapy crawl my_scraper
I get the error... 现在,当我运行scrapy crawl my_scraper
,出现错误...
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/path/to/my_scraper/pipelines/__init__.py", line 1, in <module>
from my_pipeline1 import MyPipeline1
ImportError: No module named 'my_pipeline1'
Not sure how I should approach this after in order to make my code maintainable in the long run. 不知道以后该如何处理才能使我的代码在长期内可维护。 Any tips / help are appreciated! 任何提示/帮助表示赞赏!
But after importing each pipeline class in pipelines/ init .py 但是在管道/ init .py中导入每个管道类之后
from my_pipeline1 import MyPipeline1
from my_pipeline2 import MyPipeline2
from my_pipeline3 import MyPipeline3
Looking at your project tree it seems like the above is completely unnecessary, in fact you don't even have anything named my_pipeline1
. 查看您的项目树,似乎上面的内容完全没有必要,实际上您甚至没有任何名为my_pipeline1
东西。
So just get rid of these lines in pipelines/__init__.py
. 因此,只需删除pipelines/__init__.py
的这些行pipelines/__init__.py
。
You can nest and have multiple packages in your package. 您可以嵌套并在包中包含多个包。 In this case you have my_spider
package which in itself contains spiders
and pipelines
packages, so you can safely import them with: 在这种情况下,您将拥有my_spider
程序包,该程序包本身包含spiders
和pipelines
程序包,因此可以使用以下命令安全地导入它们:
from myspider.pipelines.pipelines1 import SomeClass
# or in settings as
ITEM_PIPELINES = {
'myspider.pipelines.pipelines1.MyPipeline': 100,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.