I'm attempting to import sys and io:
import sys
import io
but I get the following errors:
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.
I'm trying to use Transcrypt and Wrangler to get my Python script running on Cloudflare's Workers, but this issue of being unable to import these modules (which don't exist on pypi) is stopping me from doing that.
Are there any alternatives to either of these in pypi that I could use instead? If not, how do I install them locally in my path.
The actual usage of them is as follows:
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.
which is performed to collect output from a Google BigQuery query. This ended up being the simplest method I could find to actually retrieve the output but I'm open to altering this as well.
You use very, very strange method to create string.
You don't need to use
print()
to keep it as string in variable - so you don't needio.String()
More popular is to keep all strings on list and later join them using
"\n"
Which can write as list comprehension
Less popular is to use directly string with
+=
and\n
You can even put
"\n"
directly in"col1={}, col2={}, col3={}\n"
BTW: if you have only 3 elements in
row
then you can use*
in.format(*row)
or with list comprehension
or even in one line
BTW:
If you will have to use
print()
with buffer (or with file handler) then you don't have to replacesys.stdout
but you can useprint(..., file=buffer)
But I don't know if it will work with
Transcrypt
(andWrangler
) becauseTranscrypt
may have only some part of standard modules - only modules which can be converted to JavaScript. Some functions may can't be converted because JavaScript don't have access to your disk (for security reason) and it can't use your files.