Using the Python interpreter

2. Using the Python interpreter

2.1 Call Interpreter

2.1.1 incoming parameters

The interpreter reads the command line parameters and converts them into a list of strings stored in the argv variable in the sys module. Execute the command import sys to import the module and access the list.

Test.py

import sys
print(sys.argv)

Use the command line to pass in and call Test.py:

chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py   1 2 4 5 5 6 

Run result:

['/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py', '1', '2', '4', '5', '5', '6']

The value of sys.argv[0] is the path to execute the script

Call the value of the script sys.argv[0] using a different path on the command line:

Test.py is called in absolute paths under different paths:

chenxiaoweideMacBook-Pro:~ chenxiaowei$ python /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py

/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py 

/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py

Calling Test.py with an absolute path to their sys.argv[0] is the same

Call Test.py using relative paths under different paths:

chenxiaoweideMacBook-Pro:users chenxiaowei$ python chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py  

chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python Test.py 

Test.py

Calling Test.py with relative paths is different for their sys.argv[0]

But what they have in common is what you pass to them and what they output (I don't know if it's set like this, but I can remember it like this).

Execute a Python statement from the command line:

chenxiaoweideMacBook-Pro:users chenxiaowei$ python -c "print('test')"

test
chenxiaoweideMacBook-Pro:users chenxiaowei$ python -c "import sys;print(sys.argv[0])"

-c

Use the python-c recommend command to execute the statement. The recommend is the python statement you want to execute. It is best to include it in double quotes. The string inside is enclosed in single quotes. If swapping the two is not possible under Windows, it is possible under Mac.

Use modules to execute from the command line:

chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python -m Test

/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py

Execute directly from the command line:

chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python Test.py

Test.py
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python Test.py
['/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python -m Test
['/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

In this way, there is no difference between the two, and both add the path of the current running directory to sys.path and can be executed

chenxiaoweideMacBook-Pro:TestCase chenxiaowei$ python -m Test

/usr/local/bin/python3.7: No module named Test
##################
chenxiaoweideMacBook-Pro:TestCase chenxiaowei$ python -m 2.1.1/Test

/usr/local/bin/python3.7: No module named Test

After adjusting the path up one level, it was found that the statement could not be executed because the module could not be found

Summary: The search module searches from the path of sys.path and only searches under the folder of the current path. The difference between the two is that sys.path is different. In fact, both add paths. One adds the path where the script is located, and the other adds the path where the script is currently running, so only the module will look at the next level of the path in the current path, or the module of the current path + XXX.XXX Block; and find this only on the command line. If a module imports in code through from and import does not add the current run path to sys.path, only the path of the currently running py will be included, so there will be a situation where the outermost py can execute the inner py, and the inner py cannot execute independently, because the path cannot be found, it may only be executed outsidePy module executed in relative paths layer by layer

2.1.2 Interactive Mode

By entering python into interactive mode on the command line, entering interactive mode will show >> to indicate the next instruction to be entered, and if multiple lines of instruction are to be entered, it will use (...) to remind the input.

chenxiaoweideMacBook-Pro:~ chenxiaowei$ python

Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> if 1==1:
...     print("sss")
... 
sss

2.2 Running environment for Interpreter

Character encoding for 2.2.1 source files

By default, Python source files are processed in UTF-8 encoding.

How do I change the default encoding format? Write the following special comments on the first line

# -*- coding: encoding -*-

Enoding can be any codecs supported by Python.

Look up the documentation and say: python3 is the default UTF-8 so I have no settings to display Chinese, python2 should be the default ASCII character

* In addition, there are cases where the first line is occupied and the second line is written:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

You can see that the first line is occupied by #!/usr/bin/env python3, which determines what version of Python interpreter is enabled to execute. This is the shebang line for UNIX

Expand:

#!/usr/bin/env python3 and #!/usr/bin/python3:

The first is performed by finding a path in an environment variable, and the second is equivalent to specifying which interpreter an absolute path specifies.

You can also specify a version of the python interpreter from the command line:

Test.py:

import sys
print(sys.version)

Run results from the command line:

chenxiaoweideMacBook-Pro:bin chenxiaowei$ python2.7 /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py 

2.7.10 (default, Feb 22 2019, 21:55:15) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)]
chenxiaoweideMacBook-Pro:bin chenxiaowei$ python3 /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py 

3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43) 
[Clang 6.0 (clang-600.0.57)]

You can use this command line to specify a version to validate the default encoding format above:

Test.py:

print("Harrol")

Run results separately:

chenxiaoweideMacBook-Pro:bin chenxiaowei$ python2.7 /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py 

  File "/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py", line 1
SyntaxError: Non-ASCII character '\xe5' in file /Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python Official Documents/TestCase/2.1.1/Test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
ChenxiaoweideMacBook-Pro:bin chenxiaowei$python3/Users/chenxiaowei/Desktop/Study\ PDF/Python/Python official document/TestCase/2.1.1/Test.py 

Harrol

After adding the specified encoding format:

Test.py:

# -*- coding: UTF-8 -*-
print("Harrol")

Run result:

ChenxiaoweideMacBook-Pro:bin chenxiaowei$Python 2.7/Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python official document/TestCase/2.1.1/Test.py 

Harrol
73 original articles were published, 14 were praised, 10,000 visits+
Private letter follow

Tags: Python encoding ascii Windows

Posted on Fri, 10 Jan 2020 22:57:03 -0500 by MarineX69