在unix类的操作系统才有意义。
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器。
#!/usr/bin/env python这种用法是为了防止操作系统用户没有将python装在默认的/usr/bin路径里。当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。
扩展资料:
Python是一种计算机程序设计语言。是一种动态的、面向对象的脚本语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。
Python常用的代码:,
1,生成随机数
rnd = random.randint(1,500):生成1-500之间的随机数
2,读文件
f = open("c:\\1.txt","r")
lines = f.readlines()
for line in lines
print line
3,写文件
f = open("c:\\1.txt","r+"):可读可写模式
f.write("123"):写入字符串
4正则表达式,读取tomcat的日志并打印日期
import re
regx = "\d\d\d\d-\d\d-\d+"
f = open("c:\stdout.log","r")
i = 0
for str in f.readlines():
if re.search(regx,str):
Response.write(str+"<br>")
if i>10:break
i=i+1
f.close()
5,连接数据库
import pgdb
conn = pgdb.connect
(host='localhost',databse='qingfeng',user='qingfeng',password='123')
cur = conn.cursor()
cur.execute("select * from dream")
print cur.rowcount
安装Virtualenv和Python3(因为有些Linux发行版默认没有安装Python3的)sudo pip install virtualenv
sudo apt-get install python3
建立一个新的工作环境
virtualenv --no-site-packages --python=python3.4 test_env
如果出现The executable python does not exist 错误,那么可以这样使用
virtualenv --no-site-packages --python=3.4 test_env
其中,–no-site-packages表示不包括系统全局的Python安装包,这样会更令环境更干净
–python=python3.4指定Python的版本未系统已经安装了的Python3.4
test_env是建立的环境的名称
进入环境测试并安装Django
使用source test_env/bin/activate命令进入开发环境,然后查看Python版本,再使用pip install django安装django
➜ virtualenv
➜ virtualenv ls
test_env
➜ virtualenv source test_env/bin/activate # 如果是windows则是source test_env/Script/activate
(test_env)➜ virtualenv python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'django'
>>>exit()
(test_env)➜ virtualenv pip install django
Collecting django
Using cached Django-1.7.3-py2.py3-none-any.whl
Installing collected packages: django
Successfully installed django-1.7.3
(test_env)➜ virtualenv python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>import django
>>>
import django没有发生错误,证明已经成功安装Django了