Application solution of Python scripts in other language Environments

Source: Internet
Author: User

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.

 
 
  1. 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.

 
 
  1. if __name__ == “__main__”:  
  2. 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:

 
 
  1. D: \ Projects \ Google \ pyblogpost \ 2exe \ dist> BlogPost.exe
  2. Traceback (most recent call last ):
  3. File "D: \ Projects \ Google \ pyblogpost \ 2exe \ BlogPost. py", line
    11, in <module>
  4. Import BlogConfig
  5. File "BlogConfig. py", line 5, in <module>
  6. File "ExtensionLoader_lxml_etree.py", line 12, in <module>
  7. File "lxml. etree. pyx", line 39, in init lxml. etree (src/lxml/
    Lxml. etree. c: 1399.
  8. 44)
  9. ImportError: No module named _ elementpath
  10. 8) Chinese characters are not allowed, even comments. Otherwise, the following error occurs.
  11. CodeString = fp. read ()
  12. File "C: \ Python31 \ lib \ codecs. py", line 300, in decode
  13. (Result, consumed) = self. _ buffer_decode (data, self. errors,
    Final)
  14. UnicodeDecodeError: 'utf8' codec can't decode bytes in
    Position 557-558: invalid
  15. Data
  16. 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:
  17. File "BlogConfig. py", line 1
  18. \ Ufeff #! /Usr/bin/python
  19. 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:

 
 
  1. from distutils.core import setup  
  2. import py2exe  
  3. options = {“py2exe”: {“bundle_files”: 1}}   
  4. setup(optionsoptions = options,  
  5. zipfile = None,  
  6. console=["hello.py"],)  

Python script command line execution:

 
 
  1. C:\Python26\python.exe setup.py py2exe  

The pythonscript will generate a hello.exe file in the distdirectory, Which is executable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.