简体   繁体   中英

Using environment variables in Python

I have an environment variable called %CCDeviceID%. When I run a command using python in Command Prompt, I want to be able to use this variable, and including %CCDeviceID% in the script doesn't work. How can I implement environment variables into my python script?

Thanks Ed

Use os.environ :

import os
print os.environ['CCDeviceID']

You can get a listing of all environment variables available to you simply by doing the following:

>>> import os
>>> print os.environ

os.environ is a dictionary mapping env variable name to its value In your case, you probably need to omit the % symbols. To get CCDeviceID .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM