[英]Python ImportError - cannot import name
我是Python新手,無法解決一個錯誤。 所以我有兩個文件夾“ Common”和“ Parsers”。 在“公用”文件夾中,有一個文件-FileUtils.py:
import re
def get_filename(target):
pattern = "^.*\/(.*)$"
names = re.findall(pattern, target)
return names[0]
在“解析器”文件夾中,還有一個文件-Parser.py:
import os
import urllib
import urllib.request
import re
from Common import FileUtils
#urls - list of strings
def parse_images(urls)
...
...
full_path = os.path.join(directory, FileUtils.get_filename(final_link))
...
最后在根文件夾-Main.py中:
from Parsers import Parser
def main():
url = ['http://www.example.com']
Parser.parse_images(url)
在Main.py的第一行-“從解析器導入解析器” PyCharm引發錯誤-“ ImportError:無法導入名稱'Parser'”。
有人可以解釋我在做什么嗎? 謝謝。
PS當所有代碼在一個文件中時-程序運行正常。
您是否已將文件夾設置為Python軟件包? 如果您想從目錄中導入,則需要在其中導入一個__init__.py
文件(可以使用一個空文件)。
嘗試創建一個空的Parsers/__init__.py
文件,看看是否可以解決該問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.