Analysis on the processing method of including static files in Python packaging

Time:2021-9-21

Usage scenario

  1. Pypi private library has been built, and the uploaded public library containsStatic fileIf necessary, initialize the database with SQL static files.
  2. packPython package, for others to use, but the project contains static files, such as HTML.

Solution steps

  1. Solve the problem of static file reading
  2. Solve the problem of static file packaging

environment

Python3.8

PyCharm 2020

Demo project as an example

  • Create project
  • Show static file reading problems
  • Solution
  • Show static file packaging problems
  • Solution

1. Create project

Create a demo project (text setup) with the following directory

test-setup

demo

\_\_init\_\_.pydemo.pydemo.txt

/demo/demo.txt

The text is from demo.txt.

/demo/demo.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os
def get_txt():
  "" "open with original open IO mode" ""
  with open('demo.txt', 'r', encoding='utf-8') as f:
    return f.read()
 
def get_demo_txt():
  "" "modify the method of obtaining path and open it with 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__":
  "" "in class tests, no exceptions" ""
  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.

There is no exception when executing the demo.py file in the current path (right-click run using pycharm)

2. Show the problem of reading static files

If you execute the above demo.py file in the root directory (or any other path except demo.py file), an error will be reported

?
1
2
3
4
5
6
7
8
$ 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'

Obviously

  • An error is reported by directly reading a static file using a relative path
  • Use os.path.dirname (\ _ \ _file \ _ \ _) to obtain the current Python file path and splice it without error

3. Solutions

Use os.path.dirname (\ _ \ _file \ _ \ _) to obtain the current Python file path, and then splice the static file path

Get in demo_ Txt () method is deleted. Continue with the following packaging demo

4. Show the problem of static file packaging

Packaging, creating a packaging profile

/setup.py

?
1
2
3
4
5
6
7
8
from setuptools import find_packages, setup
 
setup(
  name='demo',
  version='1.0.0',
  packages=find_packages(),
  zip_sage=False,
)

Execute package command

?
1
$ python setup.py sdist

Open the packaging information file as follows:

/demo.egg-info/SOURCES.txt

?
1
2
3
4
5
6
7
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

It can be seen that demo.txt is not in the resource file. You can actually try it. If other projects reference this file to obtain the file, the error filenotfounderror will also be reported

5. Solutions

Modify packaging profile

/setup.py

?
1
2
3
4
5
6
7
8
9
from setuptools import find_packages, setup
 
setup(
  name='demo',
  version='1.0.0',
  packages=find_packages(),
  zip_sage=False,
  include_package_data=True, #Package contains static file ID
)

Add profile

/MANIFEST.in

?
1
include demo/demo.txt

Final document directory

test-setup

demo

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

Execute the package command again

?
1
$ python setup.py sdist

Open the packaging information file as follows:

/demo.egg-info/SOURCES.txt

?
1
2
3
4
5
6
7
8
9
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

You can see that demo.txt is already in the package information, and there will be no error when referencing this package

reference material:

Creating a Source Distribution

Flask docs – Make the Project Installable

This is the end of this article on the processing methods of static files included in Python packaging. For more information about Python packaging static files, please search the previous articles of developepper or continue to browse the relevant articles below. I hope you will support developepper in the future!

Recommended Today

Seven Python code review tools recommended

althoughPythonLanguage is one of the most flexible development languages at present, but developers often abuse its flexibility and even violate relevant standards. So PythoncodeThe following common quality problems often occur: Some unused modules have been imported Function is missing arguments in various calls The appropriate format indentation is missing Missing appropriate spaces before and after […]