无法导入 sys 或 io

Can't import sys or io

我正在尝试导入 sys 和 io:

import sys
import io

但我收到以下错误:

Error while compiling (offending file last):
    File '/Users/username/worktest/index.py', line 11, at import of:
    File '/Users/username/worktest/env/lib/python3.7/site-packages/numpy/__init__.py', line 293, at import of:
    File '/Users/username/opt/anaconda3/lib/python3.7/os.py', line 1026, at import of:
    File 'io', line 95, namely:
    
    Import error, can't find any of:
        /Users/username/worktest/_io/_WindowsConsoleIO.py
        /Users/username/worktest/_io/_WindowsConsoleIO.js
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io/_WindowsConsoleIO.py
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io/_WindowsConsoleIO.js
        /Users/username/worktest/_io/_WindowsConsoleIO.py
        /Users/username/worktest/_io/_WindowsConsoleIO.js
        /Users/username/opt/anaconda3/lib/python3.7/_io/_WindowsConsoleIO.py
        /Users/username/opt/anaconda3/lib/python3.7/_io/_WindowsConsoleIO.js
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io/_WindowsConsoleIO.py
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io/_WindowsConsoleIO.js
        /Users/username/worktest/env/lib/python3.7/site-packages/_io/_WindowsConsoleIO.py
        /Users/username/worktest/env/lib/python3.7/site-packages/_io/_WindowsConsoleIO.js
        /Users/username/worktest/_io.py
        /Users/username/worktest/_io.js
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io.py
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io.js
        /Users/username/worktest/_io.py
        /Users/username/worktest/_io.js
        /Users/username/opt/anaconda3/lib/python3.7/_io.py
        /Users/username/opt/anaconda3/lib/python3.7/_io.js
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io.py
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io.js
        /Users/username/worktest/env/lib/python3.7/site-packages/_io.py
        /Users/username/worktest/env/lib/python3.7/site-packages/_io.js


    Aborted
    
  ./index.py
Module build failed (from ./node_modules/transcrypt-loader/__target_es5__/index.js):
Error: Command failed: python3 -m transcrypt --nomin --map --verbose "index"
    at checkExecSyncError (child_process.js:630:11)
    at Object.execSync (child_process.js:666:15)
    at Object.main (/Users/username/worktest/node_modules/transcrypt-loader/__target_es5__/index.js:56:67)
Error: webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config.

我正在尝试使用 Transcrypt 和 Wrangler 在 Cloudflare 的 Workers 上获取我的 Python 脚本 运行,但是这个问题无法导入这些模块(pypi 上不存在) ) 阻止我这样做。

在 pypi 中是否有任何替代品可供我使用?如果没有,我如何在本地路径中安装它们。

它们的实际用法如下:

old_stdout = sys.stdout # Memorize the default stdout stream
sys.stdout = buffer = io.StringIO()

for row in query_job:
    # Row values can be accessed by field name or index.
    print("col1={}, col2={}, col3={}".format(row[0], row[1], row[2]))

sys.stdout = old_stdout # Put the old stream back in place

out1 = buffer.getvalue() # Return a str containing the entire contents of the buffer.

执行此操作是为了收集 Google BigQuery 查询的输出。这最终成为我能找到的最简单的方法来实际检索输出,但我也愿意改变它。

你用了非常非常奇怪的方法来创建字符串。

您不需要使用 print() 将其作为字符串保存在变量中 - 因此您不需要 io.String()


更流行的是将所有字符串保留在列表中,然后使用 "\n"

加入它们
all_rows = []

for row in query_job:
    all_rows.append( "col1={}, col2={}, col3={}".format(row[0], row[1], row[2]) )

out1 = '\n'.join(all_rows)

哪个可以写成list comprehension

all_rows = [ "col1={}, col2={}, col3={}".format(row[0], row[1], row[2]) for row in query_job ]

out1 = '\n'.join(all_rows)

不太流行的是直接使用字符串与+=\n

out1 = ""

for row in query_job:
     out1 += "col1={}, col2={}, col3={}".format(row[0], row[1], row[2])
     out1 += "\n"

你甚至可以把"\n"直接放在"col1={}, col2={}, col3={}\n"

out1 = ""

for row in query_job:
     out1 += "col1={}, col2={}, col3={}\n".format(row[0], row[1], row[2])

顺便说一句: 如果你在 row 中只有 3 个元素,那么你可以在 .format(*row)

中使用 *
out1 = ""

for row in query_job:
     out1 += "col1={}, col2={}, col3={}\n".format(*row)

或使用列表理解

all_rows = [ "col1={}, col2={}, col3={}".format(*row) for row in query_job ]

out1 = '\n'.join(all_rows)

甚至一行

out1 = '\n'.join([ "col1={}, col2={}, col3={}".format(*row) for row in query_job ])

顺便说一句:

如果您必须将 print() 与缓冲区(或文件处理程序)一起使用,那么您不必替换 sys.stdout,但您可以使用 print(..., file=buffer)

buffer = io.StringIO()

for row in query_job:
    print("col1={}, col2={}, col3={}".format(row[0], row[1], row[2]), file=buffer)

out1 = buffer.getvalue() # 

但我不知道它是否适用于 Transcrypt(和 Wrangler),因为 Transcrypt 可能只有部分标准模块 - 只有可以转换的模块到 JavaScript。某些功能可能无法转换,因为 JavaScript 无法访问您的磁盘(出于安全原因)并且它无法使用您的文件。

能够使用 pandas:

修改以前的(非常低效的)收集输出的方法
df = pd.read_gbq(sql, project_id=project_id)

我以前不知道的更快更简单的解决方案。目前仍然与 Transcrypt 不兼容,但至少这部分已经解决了。