posted on 2024-11-02 14:19 read(830) comment(0) like(24) collect(5)
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.
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
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.
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
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.
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
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
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
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
@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
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.
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.
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
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.
Two treatment methods:
import sys import os sys.path.append(os.getcwd())
'run
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:
Fundamentally and elegantly solve the Python module import problem in VSCode - CSDN 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.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!