简体   繁体   English

python中的环境变量

[英]Environment variables in python

I want to set the environment variable for python. 我想为python设置环境变量。 I had did all the stuff as to add in system variable under system path or even pythonpath. 我做了所有的工作,以在系统路径甚至pythonpath下添加系统变量。 Even same under user variable but continuously getting error " PYTHONPATH ' is not recognized as an internal or external command, operable program or batch file." 即使在用户变量下相同,但持续出现错误“ PYTHONPATH '不被识别为内部或外部命令,可操作程序或批处理文件。”

By the type of error you are facing It's clear that you have problem installing python on windows . 根据您面临的错误类型,很明显, 在Windows上安装python时遇到问题 You did installed it, But it's env variables is not set and in your cmd you can not access it and gave you that error. 您确实安装了它,但是未设置env变量,并且在cmd中无法访问它,并给了您该错误。

Follow this instruction Add Python to the PATH Environmental Variable 请按照以下说明将Python添加到PATH环境变量中

Also If you want to work with environment variables in python, you should use os module. 另外,如果要在python中使用环境变量,则应使用os模块。 like this: 像这样:

to set value: 设置值:

os.environ['myEnv'] = 'something'

to get value: os.environ.get('myEnv', 'defaultValue') 获取值:os.environ.get('myEnv','defaultValue')

And remember if you set an environment variable, that variable is accessed only in context of your python code and will not be global for other applications. 请记住,如果设置了环境变量,则仅在您的python代码的上下文中访问该变量,而对于其他应用程序则不会全局访问该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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