简体   繁体   English

导入错误:在__init__.py中明确声明后无法导入function

[英]Import Error: Cannot import function after clearly declaring in the __init__.py

Say, I have three files __init__.py , time.py and readers.py in a directory analogpy .比如说,我在一个目录analogpy中有三个文件__init__.pytime.pyreaders.py

time.py has a class and readers.py has function read_file() time.py有一个 class 和readers.py有 function read_file()

__init__.py has following __init__.py有以下内容

from analogpy import time
from analogpy import readers

when I import readers from analogpy and call the function read_file() , it shows the following error当我从analogpy导入阅读器并调用 function read_file()时,它显示以下错误

from analogpy import readers
ImportError: cannot import name 'read_file' from 'analogpy' (analogpy/__init__.py)

Functions are clearly defined because I see they work fine when compiling individual files.函数定义明确,因为我发现它们在编译单个文件时工作正常。 What am I not understanding here?我在这里不明白什么?

  1. you can update your __init__.py like this:你可以像这样更新你的__init__.py
from .readers import read_file

then you can from analogpy import readfile in other file and call the function然后您可以从analogpy导入其他文件中的readfile并调用function

  1. or you can use或者你可以使用
from analogpy import readers

readers.read_file()

to call the function致电 function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM