对于我的烧瓶应用程序,我正在使用python-dotenv加载以下环境变量
from flask import Flask
from dotenv import load_dotenv
import os
import pprint
load_dotenv('.env_original')
print os.getenv('SQL_USER')
print '**************'
pprint.pprint(dict(os.environ))
print '**************'
app = Flask(__name__)
@app.route('/')
def index():
return 'hello'
if __name__ == '__main__':
app.run(debug=True)
但是我遇到了以下错误:
ate
**************
{'ALLUSERSPROFILE': 'C:\\ProgramData',
'APPDATA': 'C:\\Users\\xxxxx\\AppData\\Roaming',
'CHOCOLATEYINSTALL': 'C:\\ProgramData\\chocolatey',
'CHOCOLATEYLASTPATHUPDATE': 'Tue Jun 26 11:40:13 2018',
'CHOCOLATEYTOOLSLOCATION': 'C:\\tools',
'COMMONPROGRAMFILES': 'C:\\Program Files (x86)\\Common Files',
'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files',
'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files',
'COMPUTERNAME': '0217RDNAB73',
'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe',
'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData',
'HOMEDRIVE': 'C:',
'HOMEPATH': '\\Users\\xxxxx',
'KMP_DUPLICATE_LIB_OK': 'TRUE',
'LOCALAPPDATA': 'C:\\Users\\xxxxx\\AppData\\Local',
'LOGONSERVER': '\\\\KIRBY',
'MKL_SERIAL': 'YES',
'NUMBER_OF_PROCESSORS': '4',
'ONEDRIVE': 'C:\\Users\\Administrator\\OneDrive',
'OS': 'Windows_NT',
'PATH': 'C:\\Users\\xxxxx\\.virtualenvs\\fabric-49Df8aha\\Scripts;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\nodejs\\;C:\\Program Files (x86)\\IBM\\Client Access\\Emulator;C:\\Program Files (x86)\\IBM\\Client Access\\Shared;C:\\Program Files (x86)\\IBM\\Client Access\\;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Git\\cmd;C:\\ProgramData\\chocolatey\\bin;;C:\\WINDOWS\\System32\\OpenSSH\\;C:\\Users\\xxxxx\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Program Files\\Microsoft VS Code\\bin;C:\\Program Files\\MPV;C:\\Users\\xxxxx\\AppData\\Roaming\\npm;C:\\Python27;C:\\Python27\\Scripts;C:\\cygwin64\\bin;C:\\tools\\cmder;C:\\cygwin64\\usr\\local\\bin;C:\\Users\\xxxxx\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\xxxxx\\.virtualenvs\\fabric-49Df8aha\\Lib\\site-packages\\pywin32_system32;C:\\Users\\xxxxx\\.virtualenvs\\fabric-49Df8aha\\lib\\site-packages\\pywin32_system32',
'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC',
'PIP_PYTHON_PATH': 'c:\\python27\\python.exe',
'PROCESSOR_ARCHITECTURE': 'x86',
'PROCESSOR_ARCHITEW6432': 'AMD64',
'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 94 Stepping 3, GenuineIntel',
'PROCESSOR_LEVEL': '6',
'PROCESSOR_REVISION': '5e03',
'PROGRAMDATA': 'C:\\ProgramData',
'PROGRAMFILES': 'C:\\Program Files (x86)',
'PROGRAMFILES(X86)': 'C:\\Program Files (x86)',
'PROGRAMW6432': 'C:\\Program Files',
'PROMPT': '$P$G',
'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules',
'PUBLIC': 'C:\\Users\\Public',
'PYTHONDONTWRITEBYTECODE': '1',
'SESSIONNAME': 'Console',
u'SQL_USER': u'ate',
'SYSTEMDRIVE': 'C:',
'SYSTEMROOT': 'C:\\WINDOWS',
'TEMP': 'C:\\Users\\xxxxx\\AppData\\Local\\Temp',
'TMP': 'C:\\Users\\xxxxx\\AppData\\Local\\Temp',
'USERDNSDOMAIN': 'xxxxx.xxx.xx',
'USERDOMAIN': 'xxxxx_xxxxx',
'USERDOMAIN_ROAMINGPROFILE': 'xxxxx_xxxxx',
'USERNAME': 'xxxxx',
'USERPROFILE': 'C:\\Users\\xxxxx',
'WINDIR': 'C:\\WINDOWS'}
**************
* Serving Flask app "test" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
Traceback (most recent call last):
File "test.py", line 38, in <module>
app.run(debug=True)
File "C:\Users\xxxxx\.virtualenvs\fabric-49Df8aha\lib\site-packages\flask\app.py", line 943, in run
run_simple(host, port, self, **options)
File "C:\Users\xxxxx\.virtualenvs\fabric-49Df8aha\lib\site-packages\werkzeug\serving.py", line 812, in run_simple
reloader_type)
File "C:\Users\xxxxx\.virtualenvs\fabric-49Df8aha\lib\site-packages\werkzeug\_reloader.py", line 275, in run_with_reloader
sys.exit(reloader.restart_with_reloader())
File "C:\Users\xxxxx\.virtualenvs\fabric-49Df8aha\lib\site-packages\werkzeug\_reloader.py", line 132, in restart_with_reloader
close_fds=False)
File "c:\python27\Lib\subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "c:\python27\Lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "c:\python27\Lib\subprocess.py", line 640, in _execute_child
startupinfo)
TypeError: environment can only contain strings
Mu .env_original如下所示:
SQL_USER=ate
我正在使用pipenv虚拟环境。知道为什么会发生吗?
编辑:将pprint dict输出添加到os.environ。我必须用xxxx隐藏一些细节,但这些都是字符串。 env变量以unicode的形式加载。
答案 0 :(得分:0)
正如@Martijn Pieters指出的,这是python-dotenv方面的错误,而pipenv已在此处修补了该错误, https://github.com/theskumar/python-dotenv/pull/101
使用pipenv的软件包解决了该问题。
class someclasss {
function somefunction {
$arr = array();
if(condition){
array_push($arr, array("name"=>"john"));
return $arr;
}
}
}