Python 包中的相对文件路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1011337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:15:41  来源:igfitidea点击:

Relative file paths in Python packages

pythonreferencepackagesrelative-path

提问by Alex

How do I reference a file relatively to a package's directory?

如何相对于包的目录引用文件?

My directory structure is:

我的目录结构是:

    /foo
     package1/
      resources/
      __init__.py
     package2/
      resources/
      __init__.py
     script.py

script.pyimports packages package1and package2. Although the packages can be imported by any other script on the system. How should I reference resources inside, say, package1to ensure it would work in case os.path.curdiris arbitrary?

script.py导入包package1package2. 尽管可以通过系统上的任何其他脚本导入这些包。我应该如何引用内部资源,例如,package1以确保在os.path.curdir任意情况下它可以工作?

回答by Alex Morega

If you want to reference files from the foo/package1/resourcesfolder you would want to use the __file__variable of the module. Inside foo/package1/__init__.py:

如果要从foo/package1/resources文件夹中引用文件,则需要使用__file__模块的变量。内部foo/package1/__init__.py

from os import path
resources_dir = path.join(path.dirname(__file__), 'resources')

回答by jjs

A simple/safe way to do this is using the resource_filenamemethod from pkg_resources(which is distributed with setuptools) like so:

一个简单的/安全的方式来做到这一点是利用resource_filename从方法通过pkg_resources(其与分布式setuptools的),如下所示:

from pkg_resources import resource_filename
filepath = resource_filename('package1', 'resources/thefile')

Or, if you're implementing this inside package1/___init___.py:

或者,如果您在内部实施此操作package1/___init___.py

from pkg_resources import resource_filename
filepath = resource_filename(__name__, 'resources/thefile')

This gives you a clean solution that is also (if I'm not mistaken) zip safe.

这为您提供了一个干净的解决方案,它也是(如果我没记错的话)zip 安全的。

回答by Glyph

You can be zip-safe and at the same time use a nice convenient API if you use twisted.python.modules.

如果你使用twisted.python.modules,你可以是zip-safe,同时使用一个很好的方便的API 。

For example, if I have a data.txtwith some text in it and and this sample.pyin one directory:

例如,如果我有一个data.txt包含一些文本的文件,并且sample.py在一个目录中:

from twisted.python.modules import getModule
moduleDirectory = getModule(__name__).filePath.parent()
print repr(moduleDirectory.child("data.txt").open().read())

then importing samplewill do this:

然后导入sample将执行此操作:

>>> import sample
'Hello, data!\n'
>>>

If your module is in a regular directory, getModule(__name__).filePathwill be a FilePath; if it's in a zip file, it will be a ZipPath, which supports most, but not all, of the same APIs.

如果您的模块是一个普通的目录,getModule(__name__).filePath将是一个文件路径; 如果它在一个 zip 文件中,它将是一个ZipPath,它支持大多数(但不是全部)相同的 API。

回答by Alexander Artemenko

This is a bad idea, because, if your package was installed as zipped egg, then resources can be unavailable.

这是一个坏主意,因为如果您的软件包安装为压缩蛋,那么资源可能不可用。

If you use setuptool, don't forget to add zip_safe=False to the setup.py config.

如果您使用 setuptool,请不要忘记将 zip_safe=False 添加到 setup.py 配置中。