python怎麼設置環境變數

導入包 os

importos
os.environ["NAME"]=VALUE

NAME就是環境變數名,value就是你要設定的值

同理尋找環境變數可以用

print(os.environ["NAME"])
#或
os.environ.get("NAME")

Ⅱ python os模塊怎麼得到環境變數

importos
one=os.environ.get('path')
print(one)

get()括弧裡面的內容為你需要查詢的環境變數。在Windows下,path輸出該path變數中賦值的路徑。

Ⅲ 怎樣在mac設置python的環境變數

Python設置環境變數的具體方法:
在系統變數里找到PATH,雙擊PATH,在結尾加上 ";C:\Python25"(不要引號)確定即可。接下來:運行->cmd,在命令行窗口中輸入python,即可運行。
輸入
print "Hello World!"

應該有如下結果:
>>> print "Hello World!" Hello World!

在命令行窗口中輸入python文章中Python設置環境變數的分類:Python編程
#!/usr/bin/python
#FileName:setDbgServerBridge.py
import os
path=os.environ["HOME"] + "/lib"
if os.path.exists(path)==False :
os.makedirs(path)
else:
print "exists"
dstFile=path + "/libeclipse_ct_debug_core_
utility_DbgServerBridge.so"
srcFile="./libeclipse_ct_debug_core_
utility_DbgServerBridge.so"
commandLine = "cp ./libeclipse_ct_debug_
core_utility_DbgServerBridge.so " + dstFile
os.system(commandLine)
os.environ["LD_LIBRARY_PATH"]=path
以上就是對Python設置環境變數的具體方法。

Ⅳ linux python os.environ 為什麼不一樣

這個獲取的是執行程序所在的操作系統的環境變數,當然不一樣

Ⅳ python需要設置哪些環境變數我只知道一個PYTHONHOME指向安裝目錄。

1、首先,右鍵點擊-計算機(此電腦),點擊進入屬性,如圖所示。

Ⅵ python os模塊怎麼使用

一、os模塊概述

Python os模塊包含普遍的操作系統功能。如果你希望你的程序能夠與平台無關的話,這個模塊是尤為重要的。(一語中的)

二、常用方法

1、os.name

輸出字元串指示正在使用的平台。如果是window 則用'nt'表示,對於Linux/Unix用戶,它是'posix'。

2、os.getcwd()

函數得到當前工作目錄,即當前Python腳本工作的目錄路徑。

3、os.listdir()

返回指定目錄下的所有文件和目錄名。

>>> os.listdir(os.getcwd())
['Django', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'MySQL-python-wininst.log', 'NEWS.txt', 'PIL-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'RemoveMySQL-python.exe', 'RemovePIL.exe', 'Removesetuptools.exe', 'Scripts', 'setuptools-wininst.log', 'tcl', 'Tools', 'w9xpopen.exe']
>>>

4、os.remove()

刪除一個文件。

5、os.system()

運行shell命令。

>>> os.system('dir')
0
>>> os.system('cmd') #啟動dos

6、os.sep 可以取代操作系統特定的路徑分割符。

7、os.linesep字元串給出當前平台使用的行終止符

>>> os.linesep
'\r\n' #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
>>> os.sep
'\\' #Windows
>>>

8、os.path.split()

函數返回一個路徑的目錄名和文件名

>>> os.path.split('C:\\Python25\\abc.txt')
('C:\\Python25', 'abc.txt')

9、os.path.isfile()和os.path.isdir()函數分別檢驗給出的路徑是一個文件還是目錄。

>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile('a.txt')
False

10、os.path.exists()函數用來檢驗給出的路徑是否真地存在

>>> os.path.exists('C:\\Python25\\abc.txt')
False
>>> os.path.exists('C:\\Python25')
True
>>>

11、os.path.abspath(name):獲得絕對路徑

12、os.path.normpath(path):規范path字元串形式

13、os.path.getsize(name):獲得文件大小,如果name是目錄返回0L

14、os.path.splitext():分離文件名與擴展名

>>> os.path.splitext('a.txt')
('a', '.txt')

15、os.path.join(path,name):連接目錄與文件名或目錄

>>> os.path.join('c:\\Python','a.txt')
'c:\\Python\\a.txt'
>>> os.path.join('c:\\Python','f1')
'c:\\Python\\f1'
>>>

16、os.path.basename(path):返迴文件名

>>> os.path.basename('a.txt')
'a.txt'
>>> os.path.basename('c:\\Python\\a.txt')
'a.txt'
>>>

17、os.path.dirname(path):返迴文件路徑

>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'

Ⅶ python編程中os.environ()會列出一個字典,誰知道裡面的每個鍵是幹啥用的

操作系統的環境變數
可以在操作系統中運行set來看.
具體的作用是操作系統定義的,.和python一般沒有關系

Ⅷ python os模塊怎麼使用

常用方法:

1. os.name——判斷現在正在實用的平台,Windows 返回 『nt'; Linux 返回』posix'。

2. os.getcwd()——得到當前工作的目錄。

3. os.listdir()——指定所有目錄下所有的文件和目錄名。

例:

Ⅸ python os.putenv 是這樣用嗎

>>>>os.getenv("PATH")
>'/usr/joebin:/usr/local/bin:/usr/bin:/bin:/usr/games/bin:/usr/sbin:/sbin:~/bin'
>>>>os.putenv("PATH","/")
>>>>os.getenv("PATH")
>'/usr/joebin:/usr/local/bin:/usr/bin:/bin:/usr/games/bin:/usr/sbin:/sbin:~/bin'
>
>
>>>>os.getenv("FOO")
>>>>os.putenv("FOO","BAR")
>>>>os.getenv("FOO")

從上述我們不難看出putenv() 無效。

查看幫助,搜索了下:

Quoting (retyping) from the getenv docs, "...however, calls to putenv()
don't update os.environ, so it is actually preferable to assign to items
of os.environ."

調用putenv()無法更新os.environ

As to why, I'm not at all sure. Only that many environments don't
support putenv(). But why that should stop it working in the obvious
way ? No idea.

os.environ is not an ordinary dict, it's a "mapping object". And among
other things, when you modify os.environ, Python will call putenv.
Quoting from the os.environ docs, "If the platform supports the putenv()
function, this mapping may be used to modify the environment. putenv()
will be called automatically wehn the mapping is modified."
#當我們更新os.environ的時候,會自動調用putenv()


In other words, you shouldn't use putenv(), but instead modify os.environ.

#因此我們可以不用putenv了,以更新os.environ來實現調用putenv