【发布时间】:2019-04-26 12:00:58
【问题描述】:
我创建了一个如下所示的 python 包:
/command
/command
module.py
__main__.py
README.md
setup.py
file.txt
安装我运行:
sudo python setup.py install
现在当我打电话时
$ command
它显示了这个错误:
FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'
大概有setup.py、__main__.py和module.py
模块的内容setup.py
import setuptools
setuptools.setup(
name='command',
...
entry_points={
'console_scripts': [
'command = command.__main__:main'
]
}
)
__main__.py
from . import module
def main():
module.run('arg')
module.py
import shutil
# copy file.txt from command project into the directory where it is running
def run(arg):
shutil.copyfile('../file.txt', './file.txt')
通过以下方式安装此软件包后:
sudo python setup.py install
并在命令行调用程序
$ command
我收到以下错误
FileNotFoundError: [Errno 2] No such file or directory: '../file.txt'
如何查看和使用属于已安装包的文件,但要在运行该程序的环境中使用它?
编辑:
This a simplification of the problem you can download and test:
【问题讨论】:
-
安装包中是否存在该文件?如果是这样,您可以在模块中使用
__file__来找出该模块的绝对路径并从那里计算到数据文件的路径 -
@MichaelButscher 我不知道它是否在那里,因为它被打包在一个我无法打开的 .egg 文件中。
-
上面似乎您已经尝试安装该软件包。所以你应该能够检查文件是否也安装了。
-
我已经安装了这个包,安装没有任何错误。正是在使用它时才会出现错误。但是如果这就是您要问的,我如何检查 file.txt 是否在该已安装的软件包中..
-
使用某种文件资源管理器或使用命令行列出安装目录(和子目录)中的文件。
标签: python include installation text-files setup.py