Python 環境變量
下面幾個重要的環境變量,它應用于Python:
變量名 | 描述 |
---|---|
PYTHONPATH | PYTHONPATH是Python搜尋路徑,預設我們import的子產品都會從PYTHONPATH裡面尋找。 |
PYTHONSTARTUP | Python啟動後,先尋找PYTHONSTARTUP環境變量,然後執行此變量指定的檔案中的代碼。 |
PYTHONCASEOK | 加入PYTHONCASEOK的環境變量, 就會使python導入子產品的時候不區分大小寫. |
PYTHONHOME | 另一種子產品搜尋路徑。它通常内嵌于的PYTHONSTARTUP或PYTHONPATH目錄中,使得兩個子產品庫更容易切換。 |
通常我們會在指令行上設定PYTHONPATH。在執行腳本之前先執行設定PYTHONPATH的指令。
例如windows上,設定PYTHONPATH的指令為:
[plain] view plain copy
- SET ROOT_DIR=%~dp0
- SET PYTHONPATH=%PYTHONPATH%;%ROOT_DIR%src\main\python;%ROOT_DIR%src\test\python;%ROOT_DIR%generated\pyxb;%ROOT_DIR%
這樣就将我們在目前目錄下的src\main\python,src\test\python,generated\pyxb三個檔案夾放到pythonpath裡面了,我們就可以直接導入這三個檔案夾裡面的子產品了。
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Then under system variables I create a new Variable called
PythonPath
. In this variable I have
C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path

This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it, Please add
C:\Python27;
along with it. Else it will never work.
今天在調試Evernote SDK時, 遇到PythonPath的問題。 查了很多資料,有說用系統環境變量添加PythonPath, 有說在系統資料庫中的PythonPath添加新Default字段, 但是對于我來說都沒有效果, 很奇怪。
最後還是在代碼裡顯式添加sys.path才好用:
import sys
import hashlib
import binascii
import time
if "..\\lib" not in sys.path:
sys.path.append(r"..\\lib")
Windows 7 Professional I Modified @mongoose_za's answer to make it easier to change the python version:
- [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
- Click [New] under "System Variable"
- Variable Name: PY_HOME, Variable Value:C:\path\to\python\version
importError: No module named site - Click [OK]
- Locate the "Path" System variable and click [Edit]
-
Add the following to the existing variable:
%PY_HOME%;%PY_HOME%\Lib;%PY_HOME%\DLLs;%PY_HOME%\Lib\lib-tk;
importError: No module named site - Click [OK] to close all of the windows.
As a final sanity check open a command prompt and enter python. You should see
>python [whatever version you are using]
If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.