4.1. How to develop Python in a Windows environment

Source: Internet
Author: User
Tags python script run python script

4.1. How to develop Python in a Windows environment
4.1. How to develop Python in a Windows environment
4.1.1. What is the most primitive way to develop python?
4.1.1.1. Find a text editor, create a new. py file, write the Python code
4.1.1.2. Open cmd for windows and switch to the directory where the corresponding Python script is located
4.1.1.2.1. Method 1: Manually open cmd and CD to the corresponding path
4.1.1.2.2. Method 2: Open current dir cmd via notepad++
4.1.1.3. Run your Python script (. py file) in cmd
4.1.2. What is it like to use Python's shell for interactive development?
4.1.2.1. Command line version of Python Shell–python
4.1.2.2. Python shell–idle with graphical interface (Python GUI)
4.1.2.3. About the purpose of the Python Shell (command line or GUI version)
4.1.3. Python development with a third-party Python IDE What's going on?
4.1.3.1. Why is there a Python IDE
4.1.3.2. Some of the Python Ides that are currently common
4.1.3.3. Python's IDE and Python code Editor, Windows CMD, and other relationships
4.1.3.4. Some common problems encountered when using the IDE
4.1.3.4.1. The IDE only has the ability to open a file, and it does not mean that the file has been run in the shell
4.1.3.4.2. Need to be aware that there is code for the starting part of Python that can be run
4.1.4. Summary: What kind of environment is used to develop Python
4.1.4.1. Suggestions for Beginners: How to choose a python development environment
4.1.5. How to use Python scripts in a Windows environment
4.1.5.1. How to run in cmd under Windows BlogsToWordpress.py
4.2. How to develop Python in a Linux environment
4.3. How to develop Python in a Mac environment
Related old posts

About the "grooming" Python language

Summary and comparison of "grooming" various Python ides (Integrated development environments)

"Records" Using Python's Ide:pyscripter

"Resolved" Pyscripter Startup error: Python could not is properly initialized. We must quit.

"Records" Using Python's Ide:ulipad

"Resolved" After installing Ulipad, select Start Ulipad, results cannot start

"Records" Using Python's Ide:eclipse+pydev

Tutorial Installing Pydev in Eclipse

Tutorial Configuring the newly installed Pydev plugin in eclipse

Tutorial using Pydev for Python development in eclipse

"Resolved" Look for the hard-to-see Chinese font in Python code in Eclipse Pydev

"Resolved" Eclipse+pydev cannot debug python:unexpected IO Exception in Pydev debugger

"Record" Toss IDE tool: Aptana Studio 3

"Resolved" Aptana in Studio 3 with auto config configuration Pydev in python error: Java.io.IOException:Cannot run Program "python": CreateProcess error=2, the system cannot find the file specified

4.1. How to develop Python in a Windows environment
    • Collation summarizes the differences between Python2 (Python 2.x version) and Python3 (Python 3.x version)

Below, with the simplest Python code, just print some Python version information and system information:

#!/usr/bin/python#-*-coding:utf-8-*-"" "Function:" grooming "How to develop Python under Windows (how to run a python script) http://www.crifan.com/how _to_do_python_development_under_windows_environment Author:     crifan liversion:    2012-12-06 "  " " Import platform;   Print "Just for demo how to does Python development under Windows:";p rint "Current Python version info is%s"% (pythonversion );p rint "Uname=", uname;
4.1.1. Python's most primitive way to develop is what kind of 4.1.1.1. The most primitive find a text editor, create a new. py file, write the Python code

So this personal recommended notepad++.

Here, it is recommended to use notepad++ to write Python code, do python development, in addition to its own notepad++ as a text editor, useful, there is a, relatively speaking, the more important problem: character encoding many times, Due to the Python developer's coding of Python files, it is not very well understood that the problem resulted in many, relatively common character encodings during development so, in order to get a deeper understanding and learning, the real mastery of Python is necessary, Learn python from the beginning of the use of the right tools, so, to avoid later, some know the details, especially the file encoding, do not know and make all kinds of common mistakes and about the notepad++ itself, will not use, unfamiliar, you can refer to:

"Crifan recommended" lightweight text editor, Notepad Best alternative: notepad++

Almost everything about notepad++ can be found in the tutorials above.

because this is a new blank file, so notepad++, do not know you are Python code, can not help you automatically implement syntax highlighting,

You need to set it up manually:

languagePPython

You can see the syntax highlighting of the Python code:

Then go to save to a location.

fileSave As

In the dialog box that pops up, enter the file name you want to save:demoRunPythonUnderWindows

Tip: You can see that notepad++ automatically helps you write the .py suffix, because you set the Python syntax highlighting before.

Then select the corresponding path to save:

do not include (Chinese, space, etc.) special characters in the pathfor saving files, it is important to note that:

Python, and other languages, during development, it is best not to let the path with special characters here so-called special characters, refers to non-(ASCII) English characters, mainly including:

    • Chinese characters
    • Space
    • Other special characters

It contains only ordinary English characters, which are alphanumeric underscores.

For example, the path I'm here with is:

E:\Dev_Root\python\how_to_dev_python\

Otherwise, it is easy to have some anomalies during the post-development period

such as missing files, folders and other issues

And the common Chinese path is not recognized and so on.

Once saved, this is the case:

4.1.1.2. Open cmd for windows and switch to the directory where the corresponding Python script is located

And in exchange for the corresponding, Python files in the folder, there are two ways:

4.1.1.2.1. Method 1: Manually open cmd and CD to the corresponding path

And for, what is Windows cmd, how to open cmd, how to switch to the corresponding path and other content, unfamiliar words, you can refer to:

Windows command-line tools: cmd

4.1.1.2.2. Method 2: Open current dir cmd via notepad++

Here, a more convenient way to use the notepad++ function, directly open cmd, and switch to the corresponding path:

Then the direct implementation, open CMD, and switch to the corresponding path of the current file:

4.1.1.3. Run your Python script (. py file) in cmd

Then, in cmd, enter your Python script, which is the .py full file name of the file.

Here is:

demoRunPythonUnderWindows.py

Use Tab key to automate input

It is normal to manually enter the full file name of the corresponding Python, i.e., for each demoRunPythonUnderWindows.py letter entered here,

However, here is a small trick, you can take advantage of Windows (and Linux Common)Tab key, and then automatically display your current directory of files;

If the current folder has multiple files, pressing the Tab key multiple times will toggle between multiple files.

Here is a file, so by pressing the Tab Key, you can immediately display the corresponding full file name:

You can then enter a carriage return to run the corresponding Python script, and then you will see the output result in cmd:

4.1.2. What is it like to use Python's shell for interactive development?

Python has a shell that provides a Python runtime environment. Make it easy for you to develop interactively.

The Python shell, in the Windows environment, is divided into two kinds:

    • Python (command line)
    • Idle (Python GUI), when you paste multiple lines of code in idle and then run the error

4.1.2.1. Command line version of Python Shell–python

Under Windows, after installing Python, command line version of the Python shell:

4.1.2.2. Python shell–idle with graphical interface (Python GUI)

The following is the same Python shell, but it is the GUI, with the graphical interface version, as opposed to the command line version.

This is what happens when you open it:

The red TK icon in the upper left corner indicates that idle was developed using the Tkinter graphics library.

The red icon in the upper left corner, as if it were TK, is a library of graphical interfaces.

This is, in many Python tutorials,

It is commonly used as a Python development environment to teach others to write Python code that is idle.

For the Tkinter graphics library in Python, see:

Organize the graphics library in Python

In the following:

"Record" tossing the tkinter in Python

Corresponding, enter the above code, the result is similar:

4.1.2.3. About the purpose of the Python Shell (command line or GUI version)

And as a general Python development, it is seldom useful for this Python shell, either command line or GUI version of idle.

It is more suitable for testing, demonstrating the effect of some simple code execution.

The advantage is that it is convenient to see the results of code execution immediately.

So, the conclusion is:

The Python shell, whether command line or GUI, is better suited to the occasional test of a small amount of Python code, rather than the long-term development of Python.

Example 4.1. Example: Using Python's idle to do URL decoding

For example, I borrowed some of Python's library functions to implement some functions for URL decoding:

"Resolved" How to find the real address of the link if it can't be opened with Google search

About the Python editor selection

Python Editor comparison and recommendations

List a few, relative use of the more extensive (ranked in no particular order):

    • Ulipad
    • Pyscripter
    • Wing IDE
    • Eclipse + Pydev Plugin

For some of the IDE's effects, you can refer to:

Summary and comparison of "grooming" various Python ides (Integrated development environments)

4.1.3.3. Python's IDE and Python code Editor, Windows CMD, and other relationships

Below, casually find a Python IDE,

Like what

"Records" Using Python's Ide:pyscripter

By contrasting the explanations,

This makes it easier to understand the relationship between Python's original development environment and the Python IDE:

This, at least you should be for:

    • CMD for Windows
    • Python's shell
      • Command line version
      • GUI Version: IDLE
    • Python's IDE

There is a general understanding of the relationship between the three.

4.1.3.4. Some common problems encountered when using the IDE are 4.1.3.4.1. The IDE only has the ability to open the file, and it does not mean that the file has been run in the shell

Here is an example of the error, such as the one encountered here:

, which opens a file in the current IDE .py :balanced_color_module.py

According to normal logic, you should go to the menu to run the code, and then go to the corresponding console (which is estimated to be debug I/O), to see the results of the operation.

However, it is clear that for the Py file ( balanced_color_module.py ) Here, there will be no print output information even if the file is run

In addition, instead of opening the corresponding console, it opens the Python Shell, so logically:

Any code that it enters in the Python shell, in essence, does not have a half-dime relationship with the py file that you open in your current IDE.

So the result is that although some code is entered in the Python shell:

color = {"Red": 0.2, "green": 0.4, "Blue": 0.4}balanced_color (color);                

But the result was wrong and the corresponding balanced_color function was not found.

So, it is in the common sense, expected within.

Because, at this point, the things that they do are:

IDE is used, but no integrated terminal debug output is used in the IDE

It opens the Python Shell,

Because the open code in the IDE here does not have a half-penny relationship with the Python shell, the code is run in error and the corresponding function cannot be found.

4.1.3.4.2. Need to be aware that there is code for the starting part of Python that can be run

The problem here, for example, is running Python code through the IDE, but not responding:

The problems they encounter, in essence, are not related to the IDE, but people who are unfamiliar with python are still prone to make such problems in the IDE.

The cause of the error is:

No starter code to run;

The solution is:

Put the last line of the above

   Start ();

Remove indent, top line write, change to:

Start ();

You can do it. Allows Python to execute to the corresponding start function.

Of course, the best way to do this is to put:

Start ();

Then change to:

if __name__ = = "__main__":   start ();                

For specific explanations, see:

__name__ and __main__ meanings in "grooming" python

4.1.5. How to use Python scripts in a Windows environment

The first thing to say is that, as far as I know, there are two main forms that are available to users of windows in general who want to use existing Python scripts.

  1. Run a text-style Python script file directly

    Most of the Python scripts we've seen are in this form. And about the Python script, is actually a text file, you can use any text editor, such as Windows Notepad.exe to open the corresponding suffix named. py file, such as I published, for the blog moved to WordPress python scriptBlogsToWordpress.py

    And for the command line in Windows, that is, cmd runs the Python script, which is what it looks like:

    Figure 4.1. Run the Python script under the cmd under Windows


    windows cmd run python script, Do not run the Python script under the command line tool of Python

    For beginners who are unfamiliar with this usage, it is important to note that Run the Python script in cmd under Windows, and command line to write Python code in Python (once it's installed), run Python code, confuse

    With command line for Python, Take me here Python 2.7 as an example, found from the Start menu: Start Menu ⇒python 2.7⇒python (Command line),

    figure  4.2.  Python Command line


    figure  4.3. python (Command line) interface

  2. Executable files that are packaged by Python script Publishers that can run independently of Python scripts

    In this usage, the Python script Publisher must be dedicated to the Python script, specifically to use the appropriate tools to encapsulate the Python text file into executable files that can be run independently from the Python runtime environment. Plainly, is an EXE executable file, and run this EXE file, do not need to install Python, you can run.

4.1.5.1. How to run in cmd under Windows BlogsToWordpress.py

Below, take Win7 as an example, using a specially recorded GIF animation to illustrate how to run one of my Python scripts in Windows cmd:BlogsToWordpress.py

Figure 4.4. Animation shows how to run a Python script in Windows cmdBlogsToWordpress.py


Tips

The above GIF animated demo only supports HTML display in the browser. This GIF animation is not supported in other format outputs, such as in PDFs. So the following text explains the approximate flow: Use text to explain how to run a Python script in Windows cmd blogstowordpress.py

If you are viewing this article in a non-HTML format, such as a PDF, and want to see an animation, the Bash animation demonstrates how to run the Python script in Windows cmd blogstowordpress.py

A brief description of the text is:

Use text to explain how to run the Python script blogstowordpress.py in Windows CMD.

    1. Open Windows command-line environment cmd

      Start ⇒ in the "Search programs and Files" location, enter cmd, Win7 will automatically search for CMD, click on it to open cmd

    2. Switch to the corresponding location of your Python script

      You can first copy the path where your Python script is located:

      E:\Dev_Root\svn_dev_root\website\python\BlogsToWordpress

      , and then enter in CMD

      E:CD E:\Dev_Root\svn_dev_root\website\python\BlogsToWordpress

    3. Enter the script name (and parameters) to run the Python script

      Enter the appropriate python script command:

      Blogstowordpress.py-s http://blog.sina.com.cn/lifecoaching

      Then enter to run the script

4.1. How to develop Python in a Windows environment

Related Article

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.