function was not called----parsererror问题

问题来源:在ajax跨域调用网上找的一个获取手机号码归属地的接口,但是返回的数据不是严格标准的json,是那种比较老的格式,引起了jquery对于json解析的问题,这是两篇对于解决这个问题有用的贴子,先存着

function was not called----parsererror

jQuery1.4.2与老版本json格式兼容的解决方法


原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不能正常加载。(使用jQuery.getJSON()也一样)
原json文件内容为:
{
label: 'Europe (EU27)',
data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
}
解决方法一:
改成标准的json格式,要求对字符串都使用""限定,修改后的内容为:
{
"label": "Europe (EU27)",
"data": [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
}
这样就可以正常加载了。
解决方法二:
在jQuery-1.4.2.js中找到"parseJSON: function",可发现有如下代码:
复制代码 代码如下:

// Logic borrowed from http://json.org/json2.js
if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
.replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
// Try to use the native JSON parser first
return window.JSON && window.JSON.parse ?
window.JSON.parse( data ) :
(new Function("return " + data))();
} else {
jQuery.error( "Invalid JSON: " + data );
}

在httpData: function中用到了parseJSON函数:
复制代码 代码如下:

// Get the JavaScript object, if JSON is used.
if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
data = jQuery.parseJSON( data );

在jQuery1.3.2中,没有parseJSON这个方法,而是直接使用下面的代码。
复制代码 代码如下:

// Get the JavaScript object, if JSON is used.
if ( type == "json" )
data = window["eval"]("(" + data + ")");

替换成原来1.3.2的代码就可以了。
下面是其它网友的一些补充:
jquery1.4.2版本在性能上又提升了一倍,但有一个令人头痛的事就是$.getJSON函数,原先使用旧版本的JSON数据如果写得不标准,使用这个版本就无法正常获取JSON数据了
例如:
JSON不标准的写法
复制代码 代码如下:

{Err:1,errmsg:'无效ID值!请从正确表单页提交!'}

jquery1.4.x以下旧版本是能正常获取的,如果你的程序开发时用的是这类格式,那就头痛了,因为如果升级JQUERY到新版本,这种格式是读取不了的
JSON标准的写法,各种版本都能正常获取
复制代码 代码如下:

{"Err":1,"errmsg":"无效ID值!请从正确表单页提交!"}

这是因为jquery1.4.X版本里使用了native json parser,对json格式有严格的要求
如果你不想修改程序的JSON数据,还有什么方法能让旧新据适合用在新版本上呢?
方法是有的,只要恢复回旧版本的JSON处理函数就可以了,修改方法如下:
jq1.4.x Regular 版本修改
打开jquery-1.4.x.js文件,找到下面代码:
data = jQuery.parseJSON( data );
修改为以下代码:
data = window["eval"]("(" + data + ")");
jq1.4.x Minified 版本修改
打开jquery-1.4.x.min.js文件,找到下面代码:
a=c.parseJSON(a);
修改为以下代码:
a= window["eval"]("(" + a+ ")");
试试你的程序吧,呵呵,$.getJSON是不是正常了?
当然,如果你有能力写正则的话,可以修改新版本的parseJSON函数里JSON处理正则

================================分割线===================================
解析JSON和XML:jQuery.parseJSON( data )、jQuery.parseXML( data )

1.jQuery.parseJSON( data )

方法jQuery.parseJSON( data )接受一个格式良好的JSON字符串,返回解析后的JavaScript对象。如果传入残缺的JSON字符串可能导致程序抛出异常;如果不传入参数,或者传入空字符串、null、undefined,则返回null。

如果浏览器提供了原生方法JSON.parse(),则使用该方法解析JSON字符串;否则使用
( new Function( "return"+ data ) )()解析JSON字符串。

方法jQuery.parseJSON( data )的相关代码如下所示:
555 parseJSON: function( data ) {
556 if ( typeof data !== "string" || !data ) {
557 return null;
558 }
559
560 // Make sure leading/trailing whitespace is removed (IE can't handle it)
561 data = jQuery.trim( data );
562
563 // Attempt to parse using the native JSON parser first
564 if ( window.JSON && window.JSON.parse ) {
565 return window.JSON.parse( data );
566 }
567
568 // Make sure the incoming data is actual JSON
569 // Logic borrowed from http:// json.org/json2.js
570 if ( rvalidchars.test( data.replace( rvalidescape, "@" )
571 .replace( rvalidtokens, "]" )
572 .replace( rvalidbraces, "")) ) {
573
574 return ( new Function( "return " + data ) )();
575
576 }
577 jQuery.error( "Invalid JSON: " + data );
578 },

第556~561行:对于非法参数一律返回null。如果参数data不是字符串,或者可以转换为false,则返回null。

第561行:移除开头和末尾的空白符。在IE 6/7中,如果不移除就不能正确的解析,例如:
typeof ( new Function( 'return ' + '\n{}' ) )();
// 返回"undefined"

第564~566行:尝试使用原生方法JSON.parse()解析JSON字符串,并返回。

JSON对象含有两个方法:JSON.parse()和JSON.stringify(),用于JSON字符串和JavaScript对象之间的相互转换。下面是它们的语法和使用示例。

JSON.parse()解析JSON字符串为 JSON对象,其语法如下:
JSON.parse(text[, reviver])
// text 待解析为 JSON 对象的字符串
// reviver 可选。在返回解析结果前,对解析结果中的属性值进行修改
JSON.parse()的使用示例如下所示:
JSON.parse( '{ "abc": 123 }' );
// {"abc": 123 }

JSON.parse( '{ "abc": 123 }', function( key, value ){
if( key === '' ) return value;
return value * 2;
} );
// {"abc": 246 }

JSON.stringify()转换JSON对象为JSON字符串,其语法如下:
JSON.stringify( value[, replacer [, space]] )
// value 待转换为 JSON 字符串的对象
// replacer 可选。如果 replacer 是函数,转换前先执行 replacer 改变属性值,如果函数 replacer 返回 undefined,则对应的属性不会出现在结果中;如果 replacer 是数组,指定最终字符串中包含的属性集合,不在数组 replacer 中的属性不会出现在结果中
// space 增加转换后的 JSON 字符串的可读性

JSON.stringify()的使用示例如下所示:
JSON.stringify( { a: 1, b: 2 } );
// '{"a":1,"b":2}'

JSON.stringify( { a: 1, b: 2 }, function( key, value ){
if( key === '' ) return value;
if( key === 'a' ) return value * 10;
if( key === 'b' ) return undefined;
return value;
} );
// '{"a":10}'

JSON.stringify( { a: 1, b: 2 }, ['b'] );
// '{"b":2}'

JSON.stringify( { a: 1, b: 2 }, null, 4 );
// '{\n "a": 1,\n "b": 2\n}'

JSON对象、JSON.parse()、JSON.stringify()在ECMAScript 5中被标准化,IE 8以下的浏览器不支持。关于JSON规范和浏览器实现的更多信息请访问以下地址:

http://json.org/json-zh.html

http://www.ecma-international.org/publications/standards/Ecma-262.htm(ECMAScript 5第15.12节)

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON
下面回到对方法jQuery.parseJSON()的分析中来。

第570~576行:在不支持JSON.parse()的浏览器中,先检查字符串是否合法,如何合法,才会执行( new Function("return"+ data) )()并返回执行结果。检查字符串是否合法的正则和逻辑来自开源JSON解析库json2.js(https://github.com/douglascrockford/JSON-js),检测过程分为4步,用到了4个正则表达式:rvalidchars、rvalidescape、rvalidtokens、rvalidbraces,相关代码如下:
53 // JSON RegExp
54 rvalidchars = /^[\],:{}\s]*$/,
55 rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
56 rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
57 rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,

570 if ( rvalidchars.test( data.replace( rvalidescape, "@" )
571 .replace( rvalidtokens, "]" )
572 .replace( rvalidbraces, "")) ) {

第54~57行:正则rvalidescape用于匹配转义字符;正则rvalidtokens用于匹配有效值(字符串、true、false、null、数值);正则rvalidbraces用于匹配正确的左方括号“[”;正则rvalidchars用于检查字符串是否只含有指定的字符(“]”、“,”、“:”、“{”、“}”、“\s”)。

第570~572行:先利用正则rvalidescape把转义字符替换为“@”,为进行下一步替换做准备;再利用正则rvalidtokens把字符串、true、false、null、数值替换为“]”;然后利用rvalidbraces删除正确的左方括号;最后检查剩余字符是否只包含“]”、“,”、“:”、“{”、“}”、“\s”,如果只包含这些字符,那么认为JSON字符串是合法的。
第574行:通过构造函数Function()创建函数对象,然后执行。构造函数Function()的语法如下:
new Function( arguments_names..., body)
// 参见arguments_names...:任意多个字符串参数,每个字符串命名一个或多个要创建的 Function 对象的参数
// 参见body:一个字符串,指定函数的主体,可以含有任意多条 JavaScript 语句,这些语句之间用分号隔开,可以给该构造函数引用前面的参数设置的任何参数名
// 返回新创建的 Function 对象。调用该函数,将执行 body 指定的 JavaScript 代码
第577行:如果浏览器不支持JSON.parse(),并且JSON字符串不合法,则在最后抛出一个异常。
PowerShell 7 环境已加载 (版本: 7.3.6) PowerShell 7 环境已加载 (版本: 7.3.6) PS C:\Users\Administrator\Desktop> # E:\AI_System\core\config_loader.py PS C:\Users\Administrator\Desktop> import sys import: The term 'import' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> import os import: The term 'import' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> import json import: The term 'import' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> from pathlib import Path ParserError: Line | 1 | from pathlib import Path | ~~~~ | The 'from' keyword is not supported in this version of the language. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> class ConfigLoader: >> def __init__(self, config_dir=None): ParserError: Line | 2 | def __init__(self, config_dir=None): | ~ | Missing 'class' body in 'class' declaration. PS C:\Users\Administrator\Desktop> # 设置默认配置目录 PS C:\Users\Administrator\Desktop> if not config_dir: ParserError: Line | 1 | if not config_dir: | ~ | Missing '(' after 'if' in if statement. PS C:\Users\Administrator\Desktop> config_dir = Path(__file__).resolve().parent.parent / "config" __file__: The term '__file__' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> self.config_dir = Path(config_dir) config_dir: The term 'config_dir' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> self.configs = {} self.configs: The term 'self.configs' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> def load_config(self, config_name): ParserError: Line | 1 | def load_config(self, config_name): | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> """加载指定配置文件""" "加载指定配置文件" PS C:\Users\Administrator\Desktop> # 支持多种文件格式 PS C:\Users\Administrator\Desktop> possible_extensions = [".json", ".yaml", ".yml", ".py"] possible_extensions: The term 'possible_extensions' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> config_path = None config_path: The term 'config_path' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 查找存在的配置文件 PS C:\Users\Administrator\Desktop> for ext in possible_extensions: ParserError: Line | 1 | for ext in possible_extensions: | ~ | Missing opening '(' after keyword 'for'. PS C:\Users\Administrator\Desktop> test_path = self.config_dir / f"{config_name}{ext}" test_path: The term 'test_path' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> if test_path.exists(): ParserError: Line | 1 | if test_path.exists(): | ~ | Missing '(' after 'if' in if statement. PS C:\Users\Administrator\Desktop> config_path = test_path config_path: The term 'config_path' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> break PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> if not config_path: ParserError: Line | 1 | if not config_path: | ~ | Missing '(' after 'if' in if statement. PS C:\Users\Administrator\Desktop> raise FileNotFoundError(f"配置文件不存在: {config_name} in {self.config_dir}") f配置文件不存在: {config_name} in {self.config_dir}: The term 'f配置文件不存在: {config_name} in {self.config_dir}' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 根据文件类型加载配置 PS C:\Users\Administrator\Desktop> if config_path.suffix == ".json": ParserError: Line | 1 | if config_path.suffix == ".json": | ~ | Missing '(' after 'if' in if statement. PS C:\Users\Administrator\Desktop> with open(config_path, 'r', encoding='utf-8') as f: ParserError: Line | 1 | with open(config_path, 'r', encoding='utf-8') as f: | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> self.configs[config_name] = json.load(f) f: The term 'f' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> elif config_path.suffix in [".yaml", ".yml"]: elif: The term 'elif' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> try: try:: The term 'try:' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> import yaml import: The term 'import' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> with open(config_path, 'r', encoding='utf-8') as f: ParserError: Line | 1 | with open(config_path, 'r', encoding='utf-8') as f: | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> self.configs[config_name] = yaml.safe_load(f) f: The term 'f' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> except ImportError: except: The term 'except' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> raise ImportError("请安装 PyYAML 库以支持 YAML 配置") raise: The term 'raise' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> elif config_path.suffix == ".py": elif: The term 'elif' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> # 动态导入 Python 配置文件 PS C:\Users\Administrator\Desktop> spec = importlib.util.spec_from_file_location(config_name, config_path) ParserError: Line | 1 | … spec = importlib.util.spec_from_file_location(config_name, config_ … | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> module = importlib.util.module_from_spec(spec) spec: The term 'spec' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> spec.loader.exec_module(module) spec.loader.exec_module: The term 'spec.loader.exec_module' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> self.configs[config_name] = module.__dict__ self.configs[config_name]: The term 'self.configs[config_name]' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> return self.configs[config_name] self.configs[config_name]: The term 'self.configs[config_name]' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> def get_config(self, config_name, default=None): ParserError: Line | 1 | def get_config(self, config_name, default=None): | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> """获取已加载的配置""" "获取已加载的配置" PS C:\Users\Administrator\Desktop> return self.configs.get(config_name, default or {}) ParserError: Line | 1 | return self.configs.get(config_name, default or {}) | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> def reload_config(self, config_name): ParserError: Line | 1 | def reload_config(self, config_name): | ~ | Missing argument in parameter list. PS C:\Users\Administrator\Desktop> """重新加载配置""" "重新加载配置" PS C:\Users\Administrator\Desktop> if config_name in self.configs: ParserError: Line | 1 | if config_name in self.configs: | ~ | Missing '(' after 'if' in if statement. PS C:\Users\Administrator\Desktop> del self.configs[config_name] PS C:\Users\Administrator\Desktop> return self.load_config(config_name) config_name: The term 'config_name' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 创建全局配置加载器实例 PS C:\Users\Administrator\Desktop> config_loader = ConfigLoader() ParserError: Line | 1 | config_loader = ConfigLoader() | ~ | An expression was expected after '('. PS C:\Users\Administrator\Desktop> # E:\AI_System\Start-Server.ps1 PS C:\Users\Administrator\Desktop> param( >> [switch]$DebugMode, >> [int]$Port = 5000, >> [string]$Env = "dev" >> ) PS C:\Users\Administrator\Desktop> PS C:\Users\Administrator\Desktop> # 1. 设置项目根目录 PS C:\Users\Administrator\Desktop> $projectRoot = $PSScriptRoot PS C:\Users\Administrator\Desktop> Set-Location $projectRoot PS C:\Users\Administrator> PS C:\Users\Administrator> # 2. 激活虚拟环境 PS C:\Users\Administrator> $venvPath = Join-Path $projectRoot "web_ui\.venv" Join-Path: Cannot bind argument to parameter 'Path' because it is an empty string. PS C:\Users\Administrator> if (-not (Test-Path (Join-Path $venvPath "Scripts\Activate.ps1"))) { >> Write-Host "创建虚拟环境..." -ForegroundColor Yellow >> python -m venv $venvPath >> } InvalidOperation: The variable '$venvPath' cannot be retrieved because it has not been set. PS C:\Users\Administrator> PS C:\Users\Administrator> . (Join-Path $venvPath "Scripts\Activate.ps1") InvalidOperation: The variable '$venvPath' cannot be retrieved because it has not been set. PS C:\Users\Administrator> Write-Host "虚拟环境已激活 ($(python --version))" -ForegroundColor Green 虚拟环境已激活 (Python 3.10.10) PS C:\Users\Administrator> PS C:\Users\Administrator> # 3. 安装依赖 PS C:\Users\Administrator> $requirements = Join-Path $projectRoot "requirements.txt" Join-Path: Cannot bind argument to parameter 'Path' because it is an empty string. PS C:\Users\Administrator> if (Test-Path $requirements) { >> pip install -r $requirements >> } InvalidOperation: The variable '$requirements' cannot be retrieved because it has not been set. PS C:\Users\Administrator> PS C:\Users\Administrator> # 4. 设置环境变量 PS C:\Users\Administrator> $env:AI_ENV = $Env PS C:\Users\Administrator> $env:FLASK_APP = "server.py" PS C:\Users\Administrator> $env:FLASK_ENV = if ($DebugMode) { "development" } else { "production" } PS C:\Users\Administrator> PS C:\Users\Administrator> # 5. 启动服务器 PS C:\Users\Administrator> if ($DebugMode) { >> Write-Host "启动调试模式 (端口: $Port)..." -ForegroundColor Cyan >> python -m debugpy --listen 0.0.0.0:$Port -m flask run --port $Port >> } else { >> Write-Host "启动生产模式 (端口: $Port)..." -ForegroundColor Cyan >> python -m flask run --port $Port >> } 启动生产模式 (端口: 5000)... Disabling PyTorch because PyTorch >= 2.1 is required but found 2.0.0 None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used. Traceback (most recent call last): File "E:\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "E:\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "E:\Python310\lib\site-packages\flask\__main__.py", line 3, in <module> main() File "E:\Python310\lib\site-packages\flask\cli.py", line 1131, in main cli.main() File "E:\Python310\lib\site-packages\click\core.py", line 1363, in main rv = self.invoke(ctx) File "E:\Python310\lib\site-packages\click\core.py", line 1830, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "E:\Python310\lib\site-packages\click\core.py", line 1226, in invoke return ctx.invoke(self.callback, **ctx.params) File "E:\Python310\lib\site-packages\click\core.py", line 794, in invoke return callback(*args, **kwargs) File "E:\Python310\lib\site-packages\click\decorators.py", line 93, in new_func return ctx.invoke(f, obj, *args, **kwargs) File "E:\Python310\lib\site-packages\click\core.py", line 794, in invoke return callback(*args, **kwargs) File "E:\Python310\lib\site-packages\flask\cli.py", line 979, in run_command raise e from None File "E:\Python310\lib\site-packages\flask\cli.py", line 963, in run_command app: WSGIApplication = info.load_app() # pyright: ignore File "E:\Python310\lib\site-packages\flask\cli.py", line 349, in load_app app = locate_app(import_name, name) File "E:\Python310\lib\site-packages\flask\cli.py", line 245, in locate_app __import__(module_name) File "C:\Users\Administrator\server.py", line 3, in <module> from transformers import pipeline File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist File "E:\Python310\lib\site-packages\transformers\utils\import_utils.py", line 2292, in __getattr__ module = self._get_module(self._class_to_module[name]) File "E:\Python310\lib\site-packages\transformers\utils\import_utils.py", line 2322, in _get_module raise e File "E:\Python310\lib\site-packages\transformers\utils\import_utils.py", line 2320, in _get_module return importlib.import_module("." + module_name, self.__name__) File "E:\Python310\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "E:\Python310\lib\site-packages\transformers\pipelines\__init__.py", line 29, in <module> from ..models.auto.image_processing_auto import IMAGE_PROCESSOR_MAPPING, AutoImageProcessor File "E:\Python310\lib\site-packages\transformers\models\auto\image_processing_auto.py", line 28, in <module> from ...image_processing_utils_fast import BaseImageProcessorFast File "E:\Python310\lib\site-packages\transformers\image_processing_utils_fast.py", line 43, in <module> from .processing_utils import Unpack File "E:\Python310\lib\site-packages\transformers\processing_utils.py", line 36, in <module> from .audio_utils import load_audio File "E:\Python310\lib\site-packages\transformers\audio_utils.py", line 39, in <module> import soundfile as sf File "E:\Python310\lib\site-packages\soundfile.py", line 212, in <module> _snd = _ffi.dlopen(_explicit_libname) OSError: cannot load library 'libsndfile.dll': error 0x7e PS C:\Users\Administrator> # E:\AI_System\web_ui\server.py PS C:\Users\Administrator> import os import: The term 'import' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> import sys import: The term 'import' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> from pathlib import Path ParserError: Line | 1 | from pathlib import Path | ~~~~ | The 'from' keyword is not supported in this version of the language. PS C:\Users\Administrator> from flask import Flask, jsonify ParserError: Line | 1 | from flask import Flask, jsonify | ~~~~ | The 'from' keyword is not supported in this version of the language. PS C:\Users\Administrator> PS C:\Users\Administrator> # 添加项目根目录到搜索路径 PS C:\Users\Administrator> project_root = Path(__file__).resolve().parent.parent __file__: The term '__file__' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> sys.path.insert(0, str(project_root)) ParserError: Line | 1 | sys.path.insert(0, str(project_root)) | ~ | Missing expression after ','. PS C:\Users\Administrator> PS C:\Users\Administrator> print(f"项目根目录: {project_root}") f项目根目录: {project_root}: The term 'f项目根目录: {project_root}' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> PS C:\Users\Administrator> # 导入配置加载器 PS C:\Users\Administrator> from core.config_loader import config_loader ParserError: Line | 1 | from core.config_loader import config_loader | ~~~~ | The 'from' keyword is not supported in this version of the language. PS C:\Users\Administrator> PS C:\Users\Administrator> app = Flask(__name__) __name__: The term '__name__' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> PS C:\Users\Administrator> # 加载配置 PS C:\Users\Administrator> try: try:: The term 'try:' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> system_config = config_loader.load_config("system_config") system_config: The term 'system_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> hardware_config = config_loader.load_config("hardware_config") hardware_config: The term 'hardware_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> model_config = config_loader.load_config("model_registry") model_config: The term 'model_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> except Exception as e: except: The term 'except' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> print(f"配置加载错误: {e}") f配置加载错误: {e}: The term 'f配置加载错误: {e}' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> system_config = {} system_config: The term 'system_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> hardware_config = {} hardware_config: The term 'hardware_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> model_config = {} model_config: The term 'model_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> PS C:\Users\Administrator> @app.route('/') ParserError: Line | 1 | @app.route('/') | ~~~~ | The splatting operator '@' cannot be used to reference variables in an expression. '@app' can be used only as an | argument to a command. To reference variables in an expression use '$app'. PS C:\Users\Administrator> def index(): ParserError: Line | 1 | def index(): | ~ | An expression was expected after '('. PS C:\Users\Administrator> return jsonify({ >> "status": "running", >> "project_root": str(project_root), >> "system_config": system_config >> }) ParserError: Line | 2 | "status": "running", | ~ | Unexpected token ':' in expression or statement. PS C:\Users\Administrator> PS C:\Users\Administrator> @app.route('/config/<name>') ParserError: Line | 1 | @app.route('/config/<name>') | ~~~~ | The splatting operator '@' cannot be used to reference variables in an expression. '@app' can be used only as an | argument to a command. To reference variables in an expression use '$app'. PS C:\Users\Administrator> def get_config(name): name: The term 'name' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> config = config_loader.get_config(name, {}) ParserError: Line | 1 | config = config_loader.get_config(name, {}) | ~ | Missing argument in parameter list. PS C:\Users\Administrator> return jsonify(config) config: The term 'config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> PS C:\Users\Administrator> if __name__ == '__main__': ParserError: Line | 1 | if __name__ == '__main__': | ~ | Missing '(' after 'if' in if statement. PS C:\Users\Administrator> port = int(os.getenv('FLASK_PORT', 5000)) InvalidOperation: The function or command was called as if it were a method. Parameters should be separated by spaces. For information about parameters, see the about_Parameters Help topic. PS C:\Users\Administrator> app.run(host='0.0.0.0', port=port) ParserError: Line | 1 | app.run(host='0.0.0.0', port=port) | ~ | Missing argument in parameter list. PS C:\Users\Administrator> # 创建启动脚本 PS C:\Users\Administrator> $startScript = @' >> param( >> [switch]$DebugMode, >> [int]$Port = 5000, >> [string]$Env = "dev" >> ) >> >> $projectRoot = $PSScriptRoot >> Set-Location $projectRoot >> >> # ... 完整脚本内容 ... >> '@ PS C:\Users\Administrator> PS C:\Users\Administrator> Set-Content -Path "E:\AI_System\Start-Server.ps1" -Value $startScript -Encoding UTF8 PS C:\Users\Administrator> PS C:\Users\Administrator> # 创建快捷函数 PS C:\Users\Administrator> function Start-AIServer { >> param( >> [switch]$Debug, >> [int]$Port = 5000 >> ) >> >> & "E:\AI_System\Start-Server.ps1" -DebugMode:$Debug -Port $Port >> } PS C:\Users\Administrator> PS C:\Users\Administrator> # 添加到配置文件 PS C:\Users\Administrator> $profileContent = @" >> # AI 系统快捷命令 >> function Start-AIServer { >> param([switch]`$Debug, [int]`$Port = 5000) >> & "E:\AI_System\Start-Server.ps1" -DebugMode:`$Debug -Port `$Port >> } >> >> Set-Alias aistart Start-AIServer >> "@ PS C:\Users\Administrator> PS C:\Users\Administrator> Add-Content -Path $PROFILE -Value $profileContent -Encoding UTF8 PS C:\Users\Administrator> # 1. 打开 PowerShell PS C:\Users\Administrator> & "$env:ProgramFiles\PowerShell\7\pwsh.exe" PowerShell 7.3.6 A new PowerShell stable release is available: v7.5.2 Upgrade now, or check out the release page at: https://aka.ms/PowerShell-Release?tag=v7.5.2 PowerShell 7 环境已加载 (版本: 7.3.6) PS C:\Users\Administrator> # 热重载配置 PS C:\Users\Administrator> @app.route('/reload/<name>') ParserError: Line | 1 | @app.route('/reload/<name>') | ~~~~ | The splatting operator '@' cannot be used to reference variables in an expression. '@app' can be used only as an | argument to a command. To reference variables in an expression use '$app'. PS C:\Users\Administrator> def reload_config(name): name: The term 'name' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> try: try:: The term 'try:' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> config_loader.reload_config(name) name: The term 'name' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> return jsonify({"status": "success", "config": name}) ParserError: Line | 1 | return jsonify({"status": "success", "config": name}) | ~ | Unexpected token ':' in expression or statement. PS C:\Users\Administrator> except Exception as e: except: The term 'except' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> return jsonify({"status": "error", "message": str(e)}), 500 ParserError: Line | 1 | return jsonify({"status": "error", "message": str(e)}), 500 | ~ | Unexpected token ':' in expression or statement. PS C:\Users\Administrator> # 根据环境加载不同配置 PS C:\Users\Administrator> env = os.getenv("AI_ENV", "dev") env: The term 'env' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> config_loader.load_config(f"system_config_{env}") fsystem_config_{env}: The term 'fsystem_config_{env}' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> # 配置回退机制 PS C:\Users\Administrator> try: try:: The term 'try:' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> model_config = config_loader.load_config("model_registry") model_config: The term 'model_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> except FileNotFoundError: except: The term 'except' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator> model_config = config_loader.load_config("default_model_registry") model_config: The term 'model_config' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS C:\Users\Administrator>
最新发布
08-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值