2. Using the Python Interpreter
2.1 calls interpreter
2.1.1 Incoming parameters
The interpreter will read command line arguments into a string list into sys
the module argv
variable. Execute commands import sys
you can import this module and access to this list.
Test.py
import sys
print(sys.argv)
Using the command line and the incoming call Test.py:
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py 1 2 4 5 5 6
operation result:
['/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/TestCase/2.1.1/Test.py', '1', '2', '4', '5', '5', '6']
Path sys.argv [0] value of script execution
Use a different path in the command line call script sys.argv[0]
values:
In a different path absolute path to call Test.py:
chenxiaoweideMacBook-Pro:~ chenxiaowei$ python /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
Test.py absolute path to call them sys.argv[0]
are the same
At different relative path using call paths Test.py:
chenxiaoweideMacBook-Pro:users chenxiaowei$ python chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python Test.py
Test.py
Using a relative path to call Test.py their sys.argv[0]
different
But look at the two or more common is what you make him what he is output (do not know if this set, but this memory)
Execute Python statements on 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 python -c commend
the command to execute the statement, commend
what you want python statement is executed, it is best to use double quotes include up inside single quotes to wrap the string, if the two can not be exchanged in the Windows line, with Mac allows.
Use module executes the command line:
chenxiaoweideMacBook-Pro:2.1.1 chenxiaowei$ python -m Test
/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
Executed 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官方文档/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官方文档/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']
This view is no difference between the two, and both regard the path of the current directory is added to the run sys.path
in, and can be performed
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
An upward adjustment of the path, and found that this statement can not be executed because the module can not be found
Summary: Search module from sys.path
the lower path inside the search, and search only the current path of the folder difference is that sys.path
different, in fact, both have also added a path, a path where the script is added, and is currently a plus path is running, the module will only find a path under some current path, the current path or + XXX.XXX module; additionally found only in the command line is, if the code module import and import from the current run is not added to the path sys.path
of the currently running there will only be a path where py to join in, so there will be such a situation: the outermost py py can be performed inside, inside py not alone execution, since the path can not be found, one layer may be performed to the inside of the module relative path py path through the outside
2.1.2 Interactive Mode
By entering the command line python
to enter the interactive mode, enter interactive mode displays >>>
to indicate that the input of the next instruction, if you want to enter multiple lines of command, then it will use ...
to alert 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
Operating Environment 2.2 Interpreter
2.2.1 source file character encoding
By default, Python source code files to UTF-8
handle encoding.
How to modify the default encoding format it? Write the following special comment in the first line
# -*- coding: encoding -*-
encoding
It can be any of the supported codecs Python.
Access to information that is: python3
is the default UTF-8
so I did not set can also display Chinese, python2
should be the default ASCII
character
* In addition there are also the first line is occupied, in the case of the second line write:
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
You can see the first row is #!/usr/bin/env python3
occupied, its role is to enable the development of what version python解释器
to perform, this is a UNIX- shebang
line
expand:
#!/usr/bin/env python3
And #!/usr/bin/python3
the difference between:
The first is performed by finding the path environment variable, and the second which is equivalent to an absolute path is specified interpreter.
You can also specify the command line version of the python interpreter:
Test.py:
import sys
print(sys.version)
Command line operating results:
chenxiaoweideMacBook-Pro:bin chenxiaowei$ python2.7 /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/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官方文档/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 specifies the default version to verify the above encoding format:
Test.py:
print("哈咯")
Are the results:
chenxiaoweideMacBook-Pro:bin chenxiaowei$ python2.7 /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
File "/Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/TestCase/2.1.1/Test.py", line 1
SyntaxError: Non-ASCII character '\xe5' in file /Users/chenxiaowei/Desktop/Study/Study PDF/Python/Python官方文档/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/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
哈咯
After specifying the encoding format added:
Test.py:
# -*- coding: UTF-8 -*-
print("哈咯")
operation result:
chenxiaoweideMacBook-Pro:bin chenxiaowei$ python2.7 /Users/chenxiaowei/Desktop/Study/Study\ PDF/Python/Python官方文档/TestCase/2.1.1/Test.py
哈咯