使用setuptools构建项目后,应用程序找不到文件的路径

时间:2020-05-13 17:35:11

标签: python python-3.x build filepath setuptools

我正在尝试使用setuptools建立一个项目。在我的项目中显示必不可少的config.yaml文件。我已经这样任命了:

setup=(
# some stuff
    include_package_data=True,
    package_data={
        "": ["*.yaml"]
    },
# some remains stuff
)

但是当找不到config.yaml时,我遇到了问题。启动应用程序时会发生这种情况。

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\github\\Cats_Queue_Management_System\\env\\lib\\site-packages\\kitty_getter-1.0-py3.8.egg\\application\\config.yaml'

但是,它已被复制。 (据我了解)。

copying build\lib\application\config.yaml -> build\bdist.win32\egg\application

python代码中的文件交互:

# some stuff
default_file = Path(__file__).parent.parent / 'config.yaml'
    with open(default_file, 'r') as f:
        config = yaml.safe_load(f)
# some remains stuff

项目的安排如下:

Project/
 +- app/
 |    +- __init__.py
 |    +- src/
 |    |   +- __init__.py
 |    |   +- config_processing.py
 |    +- entry_point.py
 |    +- config.yaml
 +- setup.py

或更完整的版本(文件互动在settings.py处出现):

enter image description here

此外,我想指出的是,如果我直接启动该应用程序,我的意思是python entry_point.py,它可以工作。

1 个答案:

答案 0 :(得分:0)

由于该文件不存在...您的.yaml文件位于 D:\ github \ Cats_Queue_Management_System \ application \ config.yaml 上,因此您尝试在错误的路径上搜索它

试试看:

import os

fileName = "config.yaml"
appPath = r"D:\github"

for r,d,f in os.walk(appPath):
    for file in f:
        if file == fileName:
            print(os.path.abspath(file))
        else:
            print(f"File {fileName} is not exist!")

这将在所有appPath文件夹和子文件夹中搜索您的文件,如果找到该文件,则将打印该文件的绝对路径;如果不存在,则将告诉您该文件不存在。

相关问题