(编辑:jimmy 日期: 2024/12/27 浏览:2)
Python3.8
PyCharm 2020
1. 创建项目
创建一个demo项目(text-setup),目录如下
test-setup
demo
\_\_init\_\_.pydemo.pydemo.txt
/demo/demo.txt
The text is from demo.txt.
/demo/demo.py
import os def get_txt(): """使用原始打开io方式打开""" with open('demo.txt', 'r', encoding='utf-8') as f: return f.read() def get_demo_txt(): """修改获取路径方式,使用io打开""" current_dir = os.path.dirname(__file__) file_path = os.path.join(current_dir, 'demo.txt') with open(file_path, 'r', encoding='utf-8') as f: return f.read() if __name__ == "__main__": """类内测试,均无异常""" print("get_demo_txt() :", get_demo_txt()) # get_demo_txt() : The text is from demo.txt. print("get_txt() :", get_txt()) # get_txt() : The text is from demo.txt.
在当前路径下执行demo.py文件没有异常(使用PyCharm直接右键run)
2. 展示静态文件读取问题
在根目录(或其他任意除demo.py文件路径)执行上面的demo.py文件就会报错
$ python demo/demo.py get_demo_txt() : The text is from demo.txt. Traceback (most recent call last): File "demo/demo.py", line 21, in <module> print("get_txt() :", get_txt()) # get_txt() : The text is from demo.txt. File "demo/demo.py", line 6, in get_txt with open('demo.txt', 'r', encoding='utf-8') as f: FileNotFoundError: [Errno 2] No such file or directory: 'demo.txt'
明显看出
3. 解决方案
使用os.path.dirname(\_\_file\_\_)的方式去获取当前python文件路径,再去拼接静态文件的路径
demo中将get_txt()方法删除,继续进行以下打包demo
4. 展示静态文件打包问题
打包,创建打包配置文件
/setup.py
from setuptools import find_packages, setup setup( name='demo', version='1.0.0', packages=find_packages(), zip_sage=False, )
执行打包命令
$ python setup.py sdist
打开打包信息文件,内容如下
/demo.egg-info/SOURCES.txt
setup.py demo/__init__.py demo/demo.py demo.egg-info/PKG-INFO demo.egg-info/SOURCES.txt demo.egg-info/dependency_links.txt demo.egg-info/top_level.txt
可以看出,demo.txt并不在资源文件中,可以实际试一下,其他项目引用这个文件执行获取文件也将报错FileNotFoundError
5. 解决方案
修改打包配置文件
/setup.py
from setuptools import find_packages, setup setup( name='demo', version='1.0.0', packages=find_packages(), zip_sage=False, include_package_data=True, # 打包包含静态文件标识 )
增加配置文件
/MANIFEST.in
include demo/demo.txt
最终文件目录
test-setup
demo
\_\_init\_\_.pydemo.pydemo.txtsetup.pyMANIFEST.in
再次执行打包命令
$ python setup.py sdist
打开打包信息文件,内容如下
/demo.egg-info/SOURCES.txt
MANIFEST.in setup.py demo/__init__.py demo/demo.py demo/demo.txt demo.egg-info/PKG-INFO demo.egg-info/SOURCES.txt demo.egg-info/dependency_links.txt demo.egg-info/top_level.txt
可以看到demo.txt已经在打包信息当中,引用这个包也不会报错了
参考资料:
Creating a Source Distribution
Flask docs - Make the Project Installable