简体   繁体   English

在python项目中的哪里放置绝对路径?

[英]Where to place absolute paths in a python project?

I have the following project structure 我有以下项目结构

ts_tools
    /bin
    /docs
    /lib
    /ts_tools
        /data_transfer
            /tests
            data_import.py
            __init__.py
        /data_manipulation
            /tests
            data_smoothing.py
            __init__.py
        __init__.py
    config.yaml.conf
    setup.py
    LICENSE
    README.md
    TODO.md

I would like to import data with the data_import.py file from an external source. 我想从外部源使用data_import.py文件导入数据。 I use the config.yaml.conf file to specify the absolute paths of the data with: 我使用config.yaml.conf文件通过以下命令指定数据的绝对路径:

root_path:
    windows:
        data_fundamental: C:\\Users\\Malcom\\Data_Fundamental
        data_event: C:\\Users\\Malcom\\Data_Event
    linux:
        data_fundamental: /home/data/data_fundamental
        data_event: /home/data/data_event

The respective paths should be available for all tools in the ts_tools package (ie data_import.py and data_smoothing.py ). ts_tools包中的所有工具( data_import.pydata_smoothing.py )都应具有相应的路径。 Furthermore, the program should identify the os and choose the path structure accordingly. 此外,程序应识别操作系统并相应地选择路径结构。

I know how to set the paths with the yaml file using 我知道如何使用yaml文件设置路径

import yaml

with open("config.yaml.conf", "r") as ymlfile:
    cfg = yaml.load(ymlfile)

and I know how to discriminate between the os with 而且我知道如何用

if platform.system().lower() == 'windows':
    ROOT_DATA_PATH = cfg['windows']
else:
    ROOT_DATA_PATH = cfg['linux']

but I don't know where to place these code snippets. 但我不知道这些代码段的位置。 I don't think that it is appropriate to use it in the setup.py file. 我认为在setup.py文件中使用它不合适。 On the other hand I consider it inappropriate to specify a new .py file. 另一方面,我认为指定新的.py文件是不合适的。 What is a good design structure for this problem? 这个问题的良好设计结构是什么? Where should a specify absolute file paths? 应该在哪里指定绝对文件路径? Is my ansatz a step in the right direction? 我的ansatz是否朝着正确的方向迈出了一步?

Thank you in advance. 先感谢您。

In this case, you can make it relative to the home directory, so you can have ~/data_fundamental and ~/data_event (Which should be equivalent on both platforms). 在这种情况下,您可以使其相对于主目录,因此可以具有~/data_fundamental~/data_event (在两个平台上应等效)。 You can expand this with os.path.expandhome 您可以使用os.path.expandhome进行扩展

import os.path

def get_path(s):
    return os.path.normpath(os.path.normcase(
        os.path.expanduser(os.path.expandvars(s))
    ))

# get_path('~/data_fundamental') on Windows:
#    r'c:\users\malcom\data_fundamental'
# get_path('~/data_fundamental') on Linux:
#    r'/home/data/data_fundamental'
# (Assuming your username on Windows is Malcolm
#  and on Linux is data and you haven't changed
#  the default home path)

In any case, having two different setup things might be overly confusing, and you should expand ~ and %VARS% and ${VARS} anyways to make setting it up easier and run as expected. 无论如何,进行两种不同的设置可能会造成混乱,因此无论如何都应扩展~%VARS%${VARS}以使其设置更加容易并按预期运行。

Your other alternatives include: 您的其他替代方法包括:

  • Reading from environment variables 从环境变量读取
  • Writing it in setup.py (You should probably allow some way to change where the config file is as setup.py might put it in a write-protected location) setup.py写入它(您可能应该允许某种方式来更改配置文件的位置,因为setup.py可能会将其放置在写保护的位置)

You could also not have a default at all, and when not given either make a default based on sys.platform() or raise an error message telling the user to set it. 您也可能根本没有默认值,并且在没有给出默认值的情况下,要么基于sys.platform()设置默认值,要么引发一条错误消息,告诉用户进行设置。

Let's identify two different type of files/data. 让我们确定两种不同类型的文件/数据。

  • Files/data written by the user or for the user during installation/deploy 在安装/部署期间由用户或为用户编写的文件/数据
  • Files/data written by the coder 编码器写入的文件/数据

It can be okay to have absolute paths in files/data defined by the user or generated by the program executing on the user machine. 可以在用户定义的文件/数据中包含绝对路径,也可以在用户计算机上执行的程序生成绝对路径。 Absolute paths are intrinsically more fragile than relative paths, but it's not that bad in the first case. 绝对路径本质上比相对路径更脆弱,但在第一种情况下并不那么糟糕。

In the second case you should never use absolute paths. 在第二种情况下,绝对不要使用绝对路径。 I see that you are even using two different paths for windows and linux. 我看到您甚至在Windows和Linux中使用了两种不同的路径。 You don't have to do that and you shouldn't. 您不必这样做,也不应该这样做。

In Python you have things such as os.path.expanduser('~') to find the user path, or packages like appdirs . 在Python中,您可以使用os.path.expanduser('~')来查找用户路径,或者使用诸如appdirs之类的 You want to be cross-platform as much as possible, and with Python is almost always possible. 您希望尽可能地跨平台,并且使用Python几乎总是可能的。

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

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