简体   繁体   English

拥抱无法读取环境变量

[英]hug can't read environment variables

I'm trying to use a environment variables with hug. 我正在尝试使用环境变量。 However, I can't. 但是,我不能。

first step how i did: 第一步我是怎么做的:

$ export INTEGER=5

I have this in my main code: 我的主要代码中有这个:

import hug
import os


@hug.get('/')
def foo():
   var = os.environ['INTEGER']
   return {'INT':var}

when i execute 当我执行

sudo hug -p 80 -f foo.py

and go to localhost/ 并转到localhost/

Error: 错误:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/Andres/.local/share/virtualenvs/botCS-HzHaMvtf/lib/python3.6/site-packages/falcon/api.py", line 244, in __call__
    responder(req, resp, **params)
  File "/Users/Andres/.local/share/virtualenvs/botCS-HzHaMvtf/lib/python3.6/site-packages/hug/interface.py", line 734, in __call__
    raise exception
  File "/Users/Andres/.local/share/virtualenvs/botCS-HzHaMvtf/lib/python3.6/site-packages/hug/interface.py", line 709, in __call__
    self.render_content(self.call_function(input_parameters), request, response, **kwargs)
  File "/Users/Andres/.local/share/virtualenvs/botCS-HzHaMvtf/lib/python3.6/site-packages/hug/interface.py", line 649, in call_function
    return self.interface(**parameters)
  File "/Users/Andres/.local/share/virtualenvs/botCS-HzHaMvtf/lib/python3.6/site-packages/hug/interface.py", line 100, in __call__
    return __hug_internal_self._function(*args, **kwargs)
  File "repro.py", line 7, in foo
    var = os.environ['INTEGER']
  File "/Users/Andres/.local/share/virtualenvs/botCS-HzHaMvtf/bin/../lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'INTEGER'

what is wrong? 怎么了?

Your problem is that you are running hug as sudo (something you should never do btw.) and adding environment variable as you (a normal user). 您的问题是您正在以sudo身份运行hug (永远不要这样做),并以您(普通用户)的身份添加环境变量。

I'm guessing that you are running as sudo because you want to run on port 80. Run it rather on port 8080. 我猜您正在以sudo身份运行,因为您想在端口80上运行。而是在端口8080上运行。


So this works: 所以这有效:

shell: 贝壳:

export INTEGER=5

python code: python代码:

import os

@hug.get('/')
def view():
    print(os.environ['INTEGER'])  # or print(os.environ.get('INTEGER'))

shell: 贝壳:

hug -f app.py -p 8080

os.environ['INTEGER'] fails because os.environ has no key called " INTEGER ". os.environ['INTEGER']失败,因为os.environ没有名为“ INTEGER ”的键。

You can use this code, and provide an optional default as the second arg to get : 您可以使用此代码,并提供可选的默认值作为要get的第二个arg:

os.environ.get("INTEGER", 0)

This will return 0 (or whatever default you supply) if INTEGER is not found. 如果找不到INTEGER它将返回0 (或您提供的任何默认值)。

The reason INTEGER is missing must be related to where you defined your bash variable. 缺少INTEGER的原因必须与您定义bash变量的位置有关。 It must be "in scope" or accessible to the script based on where you are running the script. 它必须在“范围内”,或者根据运行脚本的位置可供脚本访问。

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

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