简体   繁体   English

我在 python 中的 os 模块有问题

[英]I have a problem with os module in python

I've recently had a problem with (i think) the os module in python:我最近遇到了(我认为)python 中的 os 模块的问题:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'consumer_key'

my code of main.py is:我的main.py代码是:

from config import getApi
import os
import sys
import time
print()



api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])

my code of config.py is我的config.py代码是

import twitter
import os

def getApi(consumer_key, consumer_secret, access_token_key, access_token_secret):
    return twitter.Api(consumer_key='*********',
        consumer_secret='*********',
        access_token_key='*********',
        access_token_secret='*********')

Send tweets with the postUpdate is possible if I write the keys in the main.py but when I put the keys in the config.py, it don't works如果我在 main.py 中编写密钥,则可以使用 postUpdate 发送推文,但是当我将密钥放入 config.py 时,它不起作用

Can anyone help me please?任何人都可以帮助我吗?

It's not an error with the os module, the keys simply aren't in the enviroment.这不是os模块的错误,键根本不在环境中。 If you're using a .env file you should use a module like dotenv to load the file.如果您使用的是.env文件,则应使用dotenv之类的模块来加载文件。

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

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