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
Relative file paths in Python packages
提问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.py
imports packages package1
and package2
. Although the packages can be imported by any other script on the system. How should I reference resources inside, say, package1
to ensure it would work in case os.path.curdir
is arbitrary?
script.py
导入包package1
和package2
. 尽管可以通过系统上的任何其他脚本导入这些包。我应该如何引用内部资源,例如,package1
以确保在os.path.curdir
任意情况下它可以工作?
回答by Alex Morega
If you want to reference files from the foo/package1/resources
folder 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_filename
method 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.txt
with some text in it and and this sample.py
in 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 sample
will do this:
然后导入sample
将执行此操作:
>>> import sample
'Hello, data!\n'
>>>
If your module is in a regular directory, getModule(__name__).filePath
will 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 配置中。