News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(8)

Python embedded packaging, that is, embed version installation and use

posted on 2024-11-02 14:19     read(830)     comment(0)     like(24)     collect(5)


1. Download and install

Download the corresponding embed version file package from the official website and unzip it. I put the interpreter python-3115-embed-amd64 and the code program folder in the same level. The file directory is as follows, and the subsequent paths are also based on this.
File Directory

2. Library loading index modification

Find python311._pth in the unzipped python environment folder (because I downloaded version 3.11.5, so here is python311, different versions have different numbers after the name), modify the content: remove the # in front of import. After completion, it is easy to retrieve the environment during operation.

# Uncomment to run site.main() automatically
#import site
修改为:
# Uncomment to run site.main() automatically
import site

3. Install pip

Download get-pip.py and put it in the same folder as python.exe in the python environment folder.
Enter the folder, for example, my decompression path is D:\python_program\python-3115-embed-amd64, then enter the folder, enter cmd in the folder path, open cmd, and run the following command:

.\python.exe get-pip.py

Two folders Lib and Scripts will be generated.

4. Configure pip Alibaba source

In the python environment folder: D:\python_program\python-3115-embed-amd64​, create a new pip.ini file and enter the content:

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

5. Add environment variables (optional)

This is an optional operation, you can add it or not. Adding environment variables can reduce the path brought in when pip is used later. It should be noted here that if other python environments are configured, it may cause interference. First remove the python environment variables of this machine.
Add three paths to the environment variable path: D:\python_program\python-3115-embed-amd64​, D:\python_program\python-3115-embed-amd64\Lib\site- packages ​, D:\python_program\python-3115-embed-amd64\Scripts​.

6. Install dependency packages

When we need to install dependencies, we must use the embedded interpreter to install them. Here is an example of installing pandas:

.\python-3115-embed-amd64\python.exe -m pip install pandas -t .\python-3115-embed-amd64\Lib\site-packages
Notice:

6.1 The interpreter must be an embedded interpreter.\python-3115-embed-amd64\python.exe, and the location of the third-party library must be specified by the -t parameter. In other words, it must be installed in the project directory, not the system's default development environment directory. If the environment variable for the embedded interpreter is configured earlier, the interpreter does not need to enter the path, which can simplify the command length.
6.2 The embed version of Python is too streamlined, and the tar package may not be installed. You can download the wheel format for direct installation. A website to find wheels: piwheels - Package List

7. Install Tkinter

Parse the Tkinter file from the standard version installation package and put it in the appropriate location. I installed python3.11.5, so I downloaded the python-3.11.5 exe installation package and unpacked it with UniExtract. The first layer was unpacked to the tcltk.msi file, and then tcltk.msi was further parsed to obtain four file packages: Lib, tcl, Dlls and libs. Put the three folders in Lib: idlelib, tkinter and turtledemo into D:\python_program\python-3115-embed-amd64\Lib\site-packages​, and the three folders tcl, Dlls and libs into D:\python_program\python-3115-embed-amd64​. And modify the python311._pth file to add the location index of the four files.

python311.zip
.
#下面四个是tk的引入包放置位置,从python安装包里拆出来的,Lib中idlelib、tkinter和turtledemo放置在site-packages里
Lib/site-packages
DLLs
libs
tcl
# Uncomment to run site.main() automatically
import site

8. Importing custom modules

After trying many code solutions on the Internet without success, I suddenly thought of an index file path itself, and I could just import the path of the custom module, saving all the coding troubles. Below is the complete version of the pth file content, and the eighth line is to import the custom module.

python311.zip
.
#下面四个是tk的引入包放置位置,从python安装包里拆出来的,Lib中idlelib、tkinter和turtledemo放置在site-packages里
Lib/site-packages
DLLs
libs
tcl
../program#自定义模块路径,这里用了..表示program文件夹与解释器所在的python-3115-embed-amd64同级。
# Uncomment to run site.main() automatically
import site

9. Write a startup script

@echo off
chcp 65001
echo 开始运行
.\python-3115-embed-amd64\python.exe      .\program\test.py
pause

Here, the chcp command is used to declare the encoding to prevent the Chinese prompt from being garbled.
Note that the interpreter must use the embedded interpreter built into the project.\python-3115-embed-amd64\python.exe

10. Problems encountered during the construction process:

10.1 Interference with local environment variables

The python environment is installed locally. When installing the dependency package for the first time, there are always abnormal errors. After deleting the local environment configuration, the errors are reduced. It is important to use the embedded interpreter .\python-3115-embed-amd64\python.exe​ to install and run, otherwise the environment interference will be chaotic.

10.2 tar format dependency package reports an error.

When installing tar format dependency packages, there is always an error and the installation fails. You can directly search for the relevant wheel package online for local installation.

10.3 Failed to read the relative path of the image.

Images with relative paths will be recognized normally in the IDE, but here a path error will be reported. Pay attention to the path hierarchy.
For example, my image is placed under \program\​, in the same folder as the running code py. In the IDE, the path is at the same level, both are current paths, and can be directly referenced by the file name. However, when running in embed, because the main path is python_program​ as the starting point, the image path needs to be added to the parent file path, which is consistent with the path method of running the py file, and the path is: .\program\xxx.png​.
My file structure is:
D:\python_program
-program
-python-3115-embed-amd64

10.4 Default save file path

The default saved file is at the starting point of the relative path of bat execution. For example, the Excel file I saved by default appears in python_program.

10.5 Module import failed in VScode

Two treatment methods:

10.5.1 The top of the code dynamically adds the current working directory to sys.path.
import sys
import os
sys.path.append(os.getcwd())
'
run
10.5.2 Modify the settings.json file of VSCode to set the environment variables of the current interpreter

I set the PYTHONPATH environment variable through VSCode's settings.json, ensuring that my project path my_project is added to the module search path.

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python 调试程序: 当前文件",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
// PYTHONPATH 环境变量
        {
    "terminal.integrated.env.windows":
		 {"PYTHONPATH": "/python_program"},
    "terminal.integrated.env.linux":
		 {"PYTHONPATH": "/path/to/your/project"},
    "terminal.integrated.env.osx":
		 {"PYTHONPATH": "/path/to/your/project"}
		}
    ]
}

Originally published at: https://www.tndyx.cc/docs/程序代码/python/2024-03-30python embedded packaging, that is, embed version installation and use/
reference:

One-click integration, all-purpose, Python3.10 project embedded one-click integration package production (Embed)_How to make python integration package-CSDN blog

Python installation package-embedded version installation and pip configuration_python_embeded-CSDN blog

Guide to creating a lightweight Python environment (including adding Tkinter:Tk support to Python Embeddable) - Zhihu (zhihu.com)

Python packaging, embedded packaging, and it is the officially recommended packaging method - Is Python embedded packaging really not as good as pyinstaller?_Python embedded version-CSDN blog

Fundamentally and elegantly solve the Python module import problem in VSCode - CSDN Blog



Category of website: technical article > Blog

Author:Abstract

link:http://www.pythonblackhole.com/blog/article/245796/121223908bc115c899e5/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

24 0
collect article
collected

Comment content: (supports up to 255 characters)