简体   繁体   English

如何设置环境变量?

[英]How do I set environment variables?

I am using Windows 10 as my os, and I went to the control panel and set two environment variables equal to an email and a password.我使用 Windows 10 作为我的操作系统,我转到控制面板并设置两个环境变量,即电子邮件和密码。 I pressed ok and went to Sublime text and set two variables equal to the environment variables but when I ran it, it says None.我按确定并转到 Sublime text 并设置两个变量等于环境变量,但是当我运行它时,它显示无。 I don't know how to fix this because I made sure the variables are there and restarted everything but it still says none.我不知道如何解决这个问题,因为我确保变量在那里并重新启动了一切,但它仍然说没有。 Here is the code.这是代码。

import os

db_user = os.environ.get('EMAIL_USER')
db_pass = os.environ.get('EMAIL_PASS')

print(db_user)
print(db_pass) 

Does anyone know what the problem is?有谁知道问题是什么?

Edit: I followed the answers and when I did them the error said, SyntaxError: cannot assign to function call And I did the DEBUSSY one and the error I had was, SyntaxError: cannot assign to function call When it said that I had a syntax error, it said it was the HOME and I checked over but it still said syntax error编辑:我按照答案进行操作时,错误说, SyntaxError:无法分配给函数调用 我做了 DEBUSSY 一个,我遇到的错误是 SyntaxError: 无法分配给函数调用 当它说我有语法时错误,它说这是家,我检查了但它仍然说语法错误

If your aim is to get the path of the home directory as argument, you can just make your user send it by making shell evaluate the argument before calling the script.如果您的目标是获取主目录的路径作为参数,您可以通过在调用脚本之前让 shell 评估参数来让您的用户发送它。

I have a simple script like -我有一个简单的脚本,比如 -

import sys
print(sys.argv[1])

In Windows I call it as -在 Windows 中,我称之为 -

set HOME=D:\
python script.py %HOME%

The output I get is -我得到的输出是 -

D:\\

In Linux -在 Linux 中 -

$ python hello.py $HOME
output -

/home/random/

If you want to get it from the environment variable, and you want to pass the environment variable to use as the first argument to the script, then you should change your script like -如果您想从环境变量中获取它,并且您想将环境变量作为第一个参数传递给脚本,那么您应该像这样更改您的脚本 -

sys.argv[1] = os.environ.get(sys.argv[1],sys.argv[1])

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

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