简体   繁体   English

从子目录中的文件 py 导入 class

[英]import a class from a file py in a subdirectory

i have a python3 project with a subdirectory GTFSr with a file gtfsr that have a class GTFSR我有一个 python3 项目,其子目录 GTFSr 的文件 gtfsr 具有 class GTFSR

---main.py  <--there is a class UTILITY
|----GTFSr
      |----gtfsrdb.py <--here i have GTFSrDB class that extend UTILITY.

when in main i type:当我主要输入:

from GTFSr.gtfsrdb import GTFSrDB

I have this error:我有这个错误:

`ImportError: cannot import name 'GTFSrDB' from 'GTFSr.gtfsrdb' (.....\GTFSr\gtfsrdb.py)`

UPDATE traceback:更新回溯:

    Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "xxx\GTFSr\gtfsrdb.py", line 33, in <module>
    from ReadProtocolBufferFiles import Utils
  File "xxx\ReadProtocolBufferFiles.py", line 9, in <module>
    from GTFSr.gtfsrdb import GTFSrDB
ImportError: cannot import name 'GTFSrDB' from 'GTFSr.gtfsrdb' (C:\xxxxxx\GTFSr\gtfsrdb.py)

Some Tips?一些技巧? thanks Regards感谢和问候

I think you need an __init__.py inside "GTFSr".我想你需要一个__init__.py里面的“GTFSr”。 Create an empty GTFSr/__init__.py and try your code please.请创建一个空的GTFSr/__init__.py并尝试您的代码。 Thanks.谢谢。

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

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