亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺析Python打包時包含靜態(tài)文件處理方法

 更新時間:2021年01月15日 10:23:16   投稿:mrr  
這篇文章主要介紹了Python打包時包含靜態(tài)文件處理方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

使用場景

  1. 已搭建了PyPI私有庫,上傳公共庫包含靜態(tài)文件,如需要使用sql靜態(tài)文件初始化數(shù)據(jù)庫。
  2. 打包python包,給其他人使用,但項目中包含靜態(tài)文件,如html。

解決步驟

  1. 解決靜態(tài)文件讀取問題
  2. 解決靜態(tài)文件打包問題

環(huán)境

Python3.8

PyCharm 2020

demo項目為例

  • 創(chuàng)建項目
  • 展示靜態(tài)文件讀取問題
  • 解決方案
  • 展示靜態(tài)文件打包問題
  • 解決方案

1. 創(chuàng)建項目

創(chuàng)建一個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__":
  """類內(nèi)測試,均無異常"""
  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.

在當前路徑下執(zhí)行demo.py文件沒有異常(使用PyCharm直接右鍵run)

2. 展示靜態(tài)文件讀取問題

在根目錄(或其他任意除demo.py文件路徑)執(zhí)行上面的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'

明顯看出

  • 直接使用相對路徑讀取靜態(tài)文件的方式報錯
  • 使用os.path.dirname(\_\_file\_\_)獲取當前python文件路徑并拼接的方式不會報錯

3. 解決方案

使用os.path.dirname(\_\_file\_\_)的方式去獲取當前python文件路徑,再去拼接靜態(tài)文件的路徑

demo中將get_txt()方法刪除,繼續(xù)進行以下打包demo

4. 展示靜態(tài)文件打包問題

打包,創(chuàng)建打包配置文件

/setup.py

from setuptools import find_packages, setup

setup(
  name='demo',
  version='1.0.0',
  packages=find_packages(),
  zip_sage=False,
)

執(zhí)行打包命令

$ python setup.py sdist

打開打包信息文件,內(nèi)容如下

/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并不在資源文件中,可以實際試一下,其他項目引用這個文件執(zhí)行獲取文件也將報錯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, # 打包包含靜態(tài)文件標識
)

增加配置文件

/MANIFEST.in

include demo/demo.txt

最終文件目錄

test-setup

demo

\_\_init\_\_.pydemo.pydemo.txtsetup.pyMANIFEST.in

再次執(zhí)行打包命令

$ python setup.py sdist

打開打包信息文件,內(nèi)容如下

/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已經(jīng)在打包信息當中,引用這個包也不會報錯了

參考資料:

Creating a Source Distribution

Flask docs - Make the Project Installable

到此這篇關(guān)于Python打包時包含靜態(tài)文件處理方法的文章就介紹到這了,更多相關(guān)Python打包靜態(tài)文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論