简体   繁体   English

使用Python 3从包含点的文件夹中进行相对导入

[英]Relative import with Python 3 from folder containing a dot

Say I have the following structure: 说我有以下结构:

main.py
.folder/
    a.py
    b.py

and using Python 3 and being currently in main.py I want to import * from a.py . 并使用Python 3并且目前在main.py我想从a.py import *

  • I assume from .folder.a import * is wrong as that ignores that the folder is actually named .folder , not folder 我假设from .folder.a import *是错误的,因为它忽略了该文件夹实际上是命名为.folder而不是folder
  • I assume from ..folder.a import * is wrong as I'm not targeting a relative parent directory called folder , but a folder named .folder within the same directory as main.py 我想from ..folder.a import *是错误的,因为我不是针对称为相对父目录folder ,而是一个文件夹命名.folder的同一目录内main.py

You can't do this; 你做不到 names of packages and modules need to be valid Python identifiers, which .folder is not. 软件包和模块的名称必须是有效的Python标识符,而.folder则不是。 You should rename your directory. 您应该重命名目录。

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

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