简体   繁体   English

正确设计Python项目

[英]Proper design of Python project

I'm planning to have project with following structure: 我打算让项目具有以下结构:

./runme.py
./my_modules/__init__.py
./my_modules/global_imports.py
./my_modules/user_defined_functions.py

Idea is to store important variables in global_imports.py from where they will be imported into runme.py using from my_modules.global_imports import * (I know it is a bad practice import modules this way, but I promise there will be just few variables with not colliding names) 想法是将重要变量存储在global_imports.py ,使用from my_modules.global_imports import *将它们导入到runme.py (我知道以这种方式导入模块是一种不好的做法,但我保证只会有很少的变量没有碰撞的名字)

Four questions: 四个问题:

  1. Two of the variables contained inside global_imports.py should be SCRIPT_PATH and SCRIPT_DIR . global_imports.py包含的两个变量应该是SCRIPT_PATHSCRIPT_DIR I've tried SCRIPT_PATH = os.path.realpath(__file__) and SCRIPT_DIR = os.path.dirname(SCRIPT_PATH) but it returns path (directory) for global_imports.py not for runme.py . 我已经尝试了SCRIPT_PATH = os.path.realpath(__file__)SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)但是它返回了global_imports.py路径(目录)而不是runme.py How can I get path (directory) of runme.py ? 如何获取runme.py路径(目录)?

  2. Inside global_imports.py I will probably import modules such as os and sys . global_imports.py里面我可能会导入ossys等模块。 I also need to import those modules inside runme.py . 我还需要在runme.py导入这些模块。 Is this considered as problem, when modules are imported first from another module and later from main script or vice versa? 当模块首先从另一个模块导入,后来从主脚本导入,反之亦然时,这是否会被视为问题?

  3. Is it possible to import variables from global_imports.py into user_defined_functions.py ? 是否可以将变量从global_imports.py导入user_defined_functions.py I consider this as bad practice I'm just curious. 我认为这是一种不好的做法,我只是好奇。

  4. Is there better approach to separate project into modules? 有没有更好的方法将项目分成模块?

Addressing your questions in order: 按顺序解决您的问题:

  1. In the first variable SCRIPT_DIR you are getting the full path of the file global_imports.py, which would be something like this: 在第一个变量SCRIPT_DIR中,您将获得文件global_imports.py的完整路径,如下所示:

     SCRIPT_PATH = '/home/../../my_project/my_modules/global_imports.py' 

    now in order to get the directory of runme.py, we should consider another variable: 现在为了获取runme.py的目录,我们应该考虑另一个变量:

     SCRIPT_PATH_DIR = os.path.dirname(SCRIPT_PATH) 

    this will give us the path 这将给我们提供道路

     SCRIPT_PATH_DIR = '/home/../../my_project/my_modules/' 

    now to get to its parent directory which contains runme.py we can get like this: 现在要访问包含runme.py的父目录,我们可以这样:

     SCRIPT_DIR = os.path.abspath(os.path.join(SCRIPT_PATH_DIR, os.pardir)) 

    Now, SCRIPT_DIR gives the path of runme.py ie: 现在,SCRIPT_DIR给出了runme.py的路径,即:

     SCRIPT_DIR = '/home/../../my_project/' 
  2. As per our project structure, runme.py should only conrtain an import to the main module and then command to run the app. 根据我们的项目结构,runme.py应该只确定对主模块的导入,然后命令运行应用程序。 So, it shouldn't contain any other imports. 因此,它不应包含任何其他导入。 Still if you need to use a module then explicitly import it in the runme.py as one of the Zen of Python says 'Explicit is better than implicit' 仍然如果你需要使用一个模块然后在runme.py中显式导入它,因为Python之一说'明确比隐含更好'

  3. Yes, it is possible, you can do it like this: 是的,有可能,你可以这样做:

     from .global_imports import variable_name 

    but in general you should have a separate config.py or settings.py file in '/../my_project/' directory which should contain all the settings and variables which you may need to use anywhere in the project. 但一般来说,你应该在'/../my_project/'目录中有一个单独的config.py或settings.py文件,它应该包含你可能需要在项目的任何地方使用的所有设置和变量。

  4. This approach is good enough as far as I've seen. 就我所见,这种方法已经足够好了。 Your main project directory contains runme.py and other modules which are used inside the project. 您的主项目目录包含runme.py和项目中使用的其他模块。 'my_modules' is one of the modules I think. 'my_modules'是我认为的模块之一。 You can have more of such modules inside the project directory. 您可以在项目目录中包含更多此类模块。 A better approach is to have settings and configurations inside one of the modules(such as my_modules) only and to have other modules for the functionality. 更好的方法是仅在其中一个模块(例如my_modules)中包含设置和配置,并为该功能提供其他模块。

Hope this helps, please comment if something is unclear. 希望这有帮助,如果有什么不清楚,请评论。

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

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