There are many ways to use Python scripts more smoothly. Let's take a look at the introduction of Python scripts. I hope you will have some gains. Package the Python scripts and libraries used as exe files to facilitate program release and Avoid installing Python on every computer of the program.
I have many related introductions on the Internet, but I seldom see Python 3. x is a successful case of packaging the Python script as exe. I have explored the success of the task and recorded it and shared it with friends in need. Welcome to the discussion.
According to your own system type and Python version download the appropriate type, I downloaded: cx_Freeze-4.1.2.win32-py3.1.msi. The latest version of this tool is 2010.1.6 and is quite new.
I want to package BlogPost. py and the modules it depends on.
A. preparations.
A. Remove all Chinese characters in the code, including comments. It does not matter if the annotation of the specified encoding is not removed)
B. If a third-party library such as lxml is used, the _ elementpath module may not be found. You need to write import _ elementpath as DONTUSE in A. py file and specify the search path for this module. Path of this module is: C: \ Python25 \ Lib \ site-packages \ lxml \ _ elementpath. py)
B. Execute the command line.
- C:\Python31\Scripts\cxfreeze.bat –include-path=C:\Python25
\Lib\site-packages\lxml –init-script=D:\Projects\Google\
pyblogpost\2exe\BlogPost.py BlogPost.py
1)-the absolute path must be used for the Startup file path specified by init-script. Otherwise, the system will prompt that the init script cannot be found.
2) You can only specify one module to be packaged, that is, the startup module.
3) All. py files cannot contain Chinese characters; otherwise, an encoding exception occurs.
4) after executing the preceding command, the dist directory will be generated, which contains the packaged executable file.
5) after the release, the executable file execution path should not contain Chinese characters or spaces ). In addition, it is best to publish all the files in the dist directory. I found that sometimes only the packaged exe cannot be run.
6) do not make the following judgment in the file to start the execution, otherwise the execution of the executable file will have no effect.
- if __name__ == “__main__”:
- main()
7) if-include-path is not specified, or the import _ elementpath as DONTUSE is not written in A. py file, the _ elementpath module cannot be found as follows:
- D: \ Projects \ Google \ pyblogpost \ 2exe \ dist> BlogPost.exe
- Traceback (most recent call last ):
- File "D: \ Projects \ Google \ pyblogpost \ 2exe \ BlogPost. py", line
11, in <module>
- Import BlogConfig
- File "BlogConfig. py", line 5, in <module>
- File "ExtensionLoader_lxml_etree.py", line 12, in <module>
- File "lxml. etree. pyx", line 39, in init lxml. etree (src/lxml/
Lxml. etree. c: 1399.
- 44)
- ImportError: No module named _ elementpath
- 8) Chinese characters are not allowed, even comments. Otherwise, the following error occurs.
- CodeString = fp. read ()
- File "C: \ Python31 \ lib \ codecs. py", line 300, in decode
- (Result, consumed) = self. _ buffer_decode (data, self. errors,
Final)
- UnicodeDecodeError: 'utf8' codec can't decode bytes in
Position 557-558: invalid
- Data
- Try to change the encoding from cp936 to UTF-8, or not, save the ASCII in the file as a UTF-8,
No. The following error occurs:
- File "BlogConfig. py", line 1
- \ Ufeff #! /Usr/bin/python
- SyntaxError: invalid character in identifier
Add another packaging method. 3rd you can use the following method to specify the parameter packaging through the command line:
1) Create the setup. py file. The content is roughly as follows: I used the method described in the above 3rd points, without modifying the parameter)
2) execute python setup. py build in the command line. This packaging method is also widely used in the sample of the cxfreeze tool.
Python 2.x
The cx_Freeze version for Python 2. x is also provided in the previous section. I guess the usage is similar and I will not repeat it here. Add this section to record the previous process of using Python 2.x to write a small tool and packaging it with py2exe.
Install the py2exe tool first.
Use the following setup. py script:
- from distutils.core import setup
- import py2exe
- options = {“py2exe”: {“bundle_files”: 1}}
- setup(optionsoptions = options,
- zipfile = None,
- console=["hello.py"],)
Python script command line execution:
- C:\Python26\python.exe setup.py py2exe
The pythonscript will generate a hello.exe file in the distdirectory, Which is executable.