python是解释型脚本语言,在执行时,逐句解释执行,不需要进行预编译。但需要有自身的Python解释器。
所以在执行Python代码时,需要指定python解释器。
指定解释器方法: 在文件开头添加搜索路径。
linux中一般安装后的默认路径如下:#!/usr/bin/env python
或者#!/usr/locat/bin/python
如果没有找到,可以通过命令查询python路径whereis python
或者which python
Windows OS
可以通过以下方式查询python路径或者第三方模块安装路径。>>> import sys
>>> sys.path
['', 'C:\\Python27\\Lib\\idlelib',
'C:\\Python27\\lib\\site-packages\\robotframework_selenium2library-1.5.0-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\docutils-0.14-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\decorator-4.2.1-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\win32',
'C:\\Python27\\lib\\site-packages\\win32\\lib',
'C:\\Python27\\lib\\site-packages\\Pythonwin',
'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']
cmd目录下运行python -V可查看python版本
修改默认python版本
先查看安装的python路径:Hansonman@localhost:
~$ whereis python
python: /usr/bin/python /usr/bin/python3.5 /usr/bin/python2.7-config /usr/bin/python3.5m /usr/bin/python2.7
/usr/lib/python3.5 /usr/lib/python2.7 /etc/python /etc/python3.5 /etc/python2.7 /usr/local/bin/python3.6m-config
/usr/local/bin/python3.6-config /usr/local/bin/python3.6m /usr/local/bin/python3.6 /usr/local/lib/python3.5
/usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.5m /usr/include/python2.7 /usr/share/python
/usr/share/man/man1/python.1.gz
使用ln -s命令来修改,命令如下:Hansonman@localhost:
~$ sudo rm /usr/bin/python
[sudo] password forHansonman:
Hansonman@localhost:
~$ sudo ln -s /usr/bin/python3.5 /usr/bin/python
justin18chan@justin18chan:
~$ python Python 3.5.2 (default, Feb 28 2018, 10:10:06)
众多python培训视频,尽在python学习网,欢迎在线学习!