Python 具有不同文件夹的最佳实践生成模块

Python 具有不同文件夹的最佳实践生成模块,python,python-3.x,Python,Python 3.x,如果我们无法从父文件夹导入脚本,那么在示例中,构建我们自己的包的推荐方法是什么 my-package/ firebase/ __init__.py setup.py #setup the firebase config auth.py #call the setup and handle all of auth user with firebase auth firestore.py #call th

如果我们无法从父文件夹导入脚本,那么在示例中,构建我们自己的包的推荐方法是什么

my-package/
   firebase/
      __init__.py
      setup.py         #setup the firebase config
      auth.py          #call the setup and handle all of auth user with firebase auth
      firestore.py     #call the setup and handle all data between firestore
   user/
      __init__.py
      user.py          #validate the auth of user
   store/
      __init__.py
      store.py         #crud post and validate the user id with user.py before store to firestore

  • 其中我有
    user.py
    脚本,该脚本需要使用firebase中的auth模块,还需要将用户数据存储到firestore

  • 我有
    store.py
    将文章数据存储到firestore,在store到firestore之前,必须验证
    user.py
    中的用户id


因此,
user.py
store.py
仍然需要从firebase导入auth和firestore,如何组织文件并从不同的目录和其他目录导入脚本?

您只需要在
\uu init\uuuuuuuuuuuy.py

之后,您可以在的帮助下在任何文件夹中访问该类obj
从firebase导入cls_obj_name

我已经尝试过了,但是它给出了一个错误ImportError:尝试在没有已知父包的情况下进行相对导入我们收到了错误,因为我们没有从根包调用用户。或者:您可以在user.py
import os中使用sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(_文件__)))
脚本通常不应该是其包的一部分。它们应该是可执行模块(
python3-m my_package.store.store
)或单独的脚本(
python3./store.py
)。如果包应该提供scripts,则使用。我的意思是脚本是文件中的python代码