在linuxpython
Linux默认是已经安装好了Python程序
目前来说,大多数的Linux发行版是安装了两个版本的Python程序
一个是Python 2.x
一个是Python 3.x
一些系统自带的程序文件需要Python 2的支持,另外Python 3又是大势所趋
所以,我们最好不要动系统的Python版本
需要使用哪个版本直接声明就可以了
运行Python的方法:
我们在Linux系统上打开终端
输入python
会弹出Python 2的版本和解释器
输入python3
会弹出Python 3的版本和解释器
所以我们一般情况下,运行python 2的程序就输入命令
python 程序文件
运行python 3的程序输入命令
python3 程序文件
这里我们默认使用版本为Python 3
在终端输入命令
Python3
弹出Python的解释器以>>>开头
我们可以在上面输入Python程序
它会直接输出结果
比如:
我们这里输入32+46,输出结果为78
输入print('hello python'),输出结果hello python
注意:当我们运行.py文件时,文件里的32+46是不会直接打印出来的
必须加打印命令
print(32+46)
我们退出python3解释器的快捷键不是Ctrl+C
而是Ctrl+Z
更多Python相关技术文章,请访问Python教程栏目进行学习!以上就是小编分享的关于怎么在linux上运行python的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!
② Linux涓嫔皢Python镄凞jango椤圭洰閮ㄧ讲鍒痨pache链嶅姟鍣
杩椤嚑澶╄姳浜嗙偣镞堕棿,灏嗘妸django寮鍙戝ソ镄剋eb椤圭洰閮ㄧ讲鍒痨pache涓婏纴鍙傝冧简瀹樻柟镄勪竴浜涙枃妗e拰浜掕仈缃戜笂镄勬枃妗o纴杩樻槸鑺变简姣旇缉澶氱殑镞堕棿锛岃繖閲屾妸閰岖疆镄勮繃绋嬭翠竴涓嬨
鏂逛究链夐渶瑕佺殑链嫔弸锛屽彲浠ュ弬钥冿纴灏戣蛋寮璺!
1. django椤圭洰閮ㄧ讲鐜澧冭存槑
镎崭綔绯荤粺 : Red Hat Enterprise Linux Server release 5.3 (Tikanga) x86_64
apache鐗堟湰 : httpd-2.2.3-22.el5
mod_wsgi鐗堟湰 : mod_wsgi-3.2-1.el5 fedora epel鍙浠ヤ笅杞
Django鐗堟湰 : 1.2.3
python 鐗堟湰 : 2.5
杩欓噷锅囧畾Django鍜孉pache宸茬粡瀹夎呭ソ锛屽苟涓挤jango镄勯”鐩宸茬粡寮鍙戝ソ銆
浠ヤ笂杞浠跺寘閮芥槸阃氲繃yum鍖呭畨瑁,杞浠跺寘閮芥槸绯荤粺镙囧嗳鐩褰旷粨鏋!
django寮鍙戝ソ镄勯”鐩鐩褰曟槸 /var/www/html/server,椤圭洰鐩褰旷粨鏋勫备笅(镙囧嗳django椤圭洰鐩褰旷粨鏋)
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#tree -d server/
server/
|-- __init__.py
|-- manage.py
|-- settings.py
|-- backend
|-- static
| |-- images
| |-- locale
| |-- plugins
| `-- themes
| |-- default
| | `-- images
| |-- gray
| | `-- images
| `-- icons
|-- template
`-- view
2. Apache鍜宫od_wsgi閰岖疆
淇鏀箇sgi閰岖疆(/etc/httpd/conf.d/wsgi.conf)
?
1
2
3
4
5
6
7
8
#cat /etc/httpd/conf.d/wsgi.conf
LoadMole wsgi_mole moles/mod_wsgi.so
WSGIScriptAlias / "/var/www/html/server/django.wsgi"
Directory "/var/www/html/server"
Order Deny,Allow
Allow from all
/Directory
椤圭洰鐩褰曚腑镄刣jango.wsgi杩欎釜鏂囦欢鏄闇瑕佹柊寤虹殑锛屽悗闱浼氲村埌濡备綍鏂板缓杩欎釜鏂囦欢銆
apache浣跨敤镄勬爣鍑嗛厤缃,Apache镄凞ocumentRoot 鎸囧悜镄勬槸 /var/www/html 鐩褰
3. 鏂板缓django.wsgi鏂囦欢
鍦ㄩ”鐩鐩褰/var/www/html/server涓嬫柊寤轰竴涓猟jango.wsgi锛屾枃浠跺唴瀹瑰备笅:
?
1
2
3
4
5
6
7
8
9
10
#cat /var/www/html/server/django.wsgi
# -*- coding: utf-8 -*-
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
current_dir = os.path.dirname(__file__)
if current_dir not in sys.path: sys.path.append(current_dir)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
绗涓夎 os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' 锛岃繖涓狲ettings灏辨槸鎸囬”鐩鐩褰曚笅镄剆etting.py鏂囦欢.
绗锲涜 os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs' 锛屾寚瀹氲В铡媏gg鏂囦欢镄刢ache鐩褰曪纴纭淇濊繍琛宎pache镄勭敤鎴凤纴鑳藉熷规ょ洰褰曟湁璇诲啓𨱒冮檺.
绗浜旓纴鍏琛 灏嗗綋鍓岖洰褰曡嚜锷ㄥ姞鍏ュ埌python镄勬悳绱㈣矾寰,濡傛灉椤圭洰涓链夎嚜宸卞啓镄勬ā鍧楋纴鏂逛究浣跨敤鍜屽彂甯
链钖庯纴杩欎釜django.wsgi鏂囦欢钖嶅彲浠ラ殢渚垮彇锛屼緥濡伥est.wsgi銆乤pp.wsgi绛夌瓑锛屼絾鏄涓瀹氲佷笌/etc/httpd/conf.d/wsgi.conf閰岖疆鏂囦欢涓閰岖疆淇濇寔涓镊淬
濡傛灉鎭ㄨ繖閲屾柊寤虹殑鏂囦欢钖崭笉鏄痉jango.wsgi钥屾槸test.wsgi锛岄偅涔/etc/httpd/conf.d/wsgi.conf涓镄勯厤缃灏卞簲璇ヤ慨鏀逛负
?
1
WSGIScriptAlias / "/var/www/html/server/test.wsgi"
4. 淇鏀筪jango椤圭洰涓镄剆etting.py鏂囦欢
镓惧埌椤圭洰鐩褰曚笅镄剆etting.py锛屽逛簬杩欓噷灏辨槸/var/www/html/server/setting.py銆傛垒鍒板叾涓镄凾EMPLATE_DIRS锛屼慨鏀逛负锛
?
1
TEMPLATE_DIRS = ("/var/www/html/server/template",)
娉ㄦ剰: 妯℃澘鐩褰曞湪杩欓噷涓瀹氲佺敤缁濆硅矾锷诧纴钥屼笉鑳界敤鐩稿硅矾寰,褰撶劧涔熸湁鏂规硶锷ㄦ佽剧疆妯℃澘璺锷
PS锛氩叧浜巑od_wsgi
鐩鍓峬od_wsgi链変袱绉嶅伐浣沧ā寮忥细
绗涓绉嶆槸宓屽叆妯″纺锛岀被浼间簬mod_python锛岀洿鎺ュ湪apache杩涚▼涓杩愯岋纴杩欐牱镄勫ソ澶勬槸涓嶉渶瑕佸彟澶栧炲姞杩涚▼锛屼絾鏄鍧忓勪篃寰堟槑鏄撅纴镓链夊唴瀛橀兘鍜宎pache鍏变韩锛屽傛灉鍜宫od_python涓镙烽犳垚鍐呭瓨婕忔礊镄勮瘽锛屽氨浼氩嵄瀹虫暣涓猘pache銆傝屼笖濡傛灉apache鏄鐢╳orker mpm锛宫od_wsgi涔熷氨寮哄埗杩涘叆浜嗙嚎绋嬫ā寮忥纴杩欐牱瀛愬逛簬闱炵嚎绋嫔畨鍏ㄧ殑绋嫔簭𨱒ヨ村氨娌℃硶鐢ㄤ简銆
杩欑嶆ā寮忎笅闇瑕佸湪apache镄剉host涓濡备笅璁剧疆锛
?
1
span style="font-family: 'times new roman', times;"WSGIScriptAlias /path /path-to-wsgi/span
鍗冲彲鐢熸晥锛屽逛簬灏忓瀷鑴氭湰镄勮瘽锛岀洿鎺ョ敤杩欑嶆ā寮忓嵆鍙銆
绗浜岀嶆槸钖庡彴妯″纺锛岀被浼间簬FastCGI镄勫悗鍙帮纴mod_wsgi浼氩焌pache镄勫栧3锛屽彟澶栧惎锷ㄤ竴涓鎴栧氢釜杩涚▼锛岀劧钖庨氲繃socket阃氢俊鍜宎pache镄勮繘绋嬭仈绯汇
杩欑嶆柟寮忓彧瑕佷娇鐢ㄤ互涓嬮厤缃鍗冲彲锛
?
1
2
3
4
5
6
7
8
9
10
#钖锷╓SGI钖庡彴锛宻ite1鏄钖庡彴钖嶅瓧
WSGIDaemonProcess site1 processes=1 threads=15 display-name=%{GROUP}
#鍒嗛厤褰揿墠涓娄笅鏂囧簲璇ヤ娇鐢ㄥ摢涓猈SGI钖庡彴锛屽彲浠ユ斁鍦↙ocation閲岄溃鎸囧畾
WSGIProcessGroup site1
#镙规嵁褰揿墠涓娄笅鏂囩殑ProcessGroup鍒嗛厤鍒板瑰簲镄勫悗鍙
WSGIScriptAlias /path /path-to-wsgi
鍦ㄨ繖绉嶆ā寮忎笅锛屾垜浠鍙浠ラ氲繃璋冭妭processes鍜宼hreads镄勫兼潵璁剧疆涓夌峂PM镄勬ā寮忥细prefork', 'worker', 'winnt'銆
winnt妯″纺
?
1
2
3
WSGIDaemonProcess example threads=25
wsgi.multithread True
wsgi.multiprocess False
姝ゆ椂processes=1锛屼絾鏄痬ultiprocess涓篺alse
濡傛灉鏄惧纺鍦版寚鍑簆rocesses涓1闾d箞锛
?
1
2
3
WSGIDaemonProcess example processes=1 threads=25
wsgi.multithread True
wsgi.multiprocess True
worker妯″纺
?
1
2
3
4
WSGIDaemonProcess example processes=2 threads=25
wsgi.multithread True
wsgi.multiprocess True
preforker妯″纺
?
1
2
3
WSGIDaemonProcess example processes=5 threads=1
wsgi.multithread False
wsgi.multiprocess True
钖庡彴妯″纺鐢变簬鏄涓巃pache杩涚▼鍒嗙讳简锛屽唴瀛樼嫭绔嬶纴钥屼笖鍙浠ョ嫭绔嬮吨钖锛屼笉浼氩奖鍝峚pache镄勮繘绋嬶纴濡傛灉浣犳湁澶氢釜椤圭洰(django)锛屽彲浠ラ夋嫨寤虹珛澶氢釜钖庡彴鎴栬呭叡钖屼娇鐢ㄤ竴涓钖庡彴銆
姣斿傚湪钖屼竴涓猇irtualHost閲岄溃锛屼笉钖岀殑path瀵瑰簲涓嶅悓镄刣jango椤圭洰锛屽彲浠ュ悓镞朵娇鐢ㄤ竴涓狣aemon锛
?
1
2
3
4
5
6
7
span style="font-family: 'times new roman', times;"WSGIDaemonProcess default processes=1 threads=1 display-name=%{GROUP}
WSGIProcessGroup default
WSGIScriptAlias /project1 钬/home/website/project1.wsgi钬
WSGIScriptAlias /project2 钬/home/website/project2.wsgi钬/span
杩欐牱瀛愪袱涓猟jango閮戒娇鐢ㄥ悓涓涓猈SGI钖庡彴銆
涔熷彲浠ユ妸涓嶅悓镄勯”鐩鍒嗗紑锛屽垎寮浣跨敤涓嶅悓镄勫悗鍙帮纴杩欐牱寮阌姣旇缉澶э纴浣嗗氨涓崭细钥﹀悎鍦ㄤ竴璧蜂简銆
display-name鏄钖庡彴杩涚▼镄勫悕瀛楋纴杩欐牱鏂逛究閲嶅惎瀵瑰簲镄勮繘绋嬶纴钥屼笉闇瑕佸叏閮ㄦ潃鎺夈
?
1
2
3
4
5
6
7
8
9
10
11
12
13
span style="font-family: 'times new roman', times;"WSGIDaemonProcess site1 processes=1 threads=1 display-name=%{GROUP}
WSGIDaemonProcess site2 processes=1 threads=1 display-name=%{GROUP}
Location 钬/project1钬
WSGIProcessGroup site1
/Location
WSGIScriptAlias /project1 钬/home/website/project1.wsgi钬
Location 钬/project1钬
WSGIProcessGroup site2
/Location
WSGIScriptAlias /project2 钬/home/website/project2.wsgi钬/span
瀵逛簬django 1.0浠ヤ笅镄勭増链锛岀敱浜庡畼鏂硅ゅ畾涓嶆槸绾跨▼瀹夊叏镄勶纴镓浠ュ缓璁浣跨敤澶氲繘绋嫔崟绾跨▼妯″纺
?
1
processes=n threads=1
瀵逛簬django 1.0浠ュ悗锛屽氨鍙浠ユ斁蹇幂殑浣跨敤澶氲繘绋嫔氱嚎绋嬫ā寮忥细
?
1
processes=2 threads=64
杩欐牱瀛愭ц兘浼氭洿濂姐
③ 如何在linux系统中安装python
linux系统中安装python包括以下滚键罩三个步骤: