Can't import sys or io

1k views Asked by At

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.

2

There are 2 answers

4
furas On BEST ANSWER

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 need io.String()


More popular is to keep all strings on list and later join them using "\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)

Which can write as list comprehension

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

out1 = '\n'.join(all_rows)

Less popular is to use directly string with += and \n

out1 = ""

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

You can even put "\n" directly in "col1={}, col2={}, col3={}\n"

out1 = ""

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

BTW: if you have only 3 elements in row then you can use * in .format(*row)

out1 = ""

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

or with list comprehension

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

out1 = '\n'.join(all_rows)

or even in one line

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

BTW:

If you will have to use print() with buffer (or with file handler) then you don't have to replace sys.stdout but you can use 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() # 

But I don't know if it will work with Transcrypt (and Wrangler) because Transcrypt 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.

2
a13a22 On

Was able to rework the previous (highly inefficient) method of collecting the output using pandas:

df = pd.read_gbq(sql, project_id=project_id)

Much faster and simpler solution that I wasn't aware of previously. Still incompatible with Transcrypt at the moment, but at least this portion is solved.