Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

import socket

Open Quphoria opened this issue 9 years ago • 6 comments

I cannot use the python socket module It says:

C:\Users\Owner\Desktop\Coding Projects\chatserver>Transcrypt import_test.py -b Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.6.4 Copyright (C) Geatec Engineering. License: Apache 2.0

Error in program import_test.py, module _socket, line 49:
        Attempt to load module: _socket
        Can't find any of:
                C:/Users/Owner/Desktop/Coding Projects/chatserver/__javascript__/_socket.mod.js
                C:/Users/Owner/Desktop/Coding Projects/chatserver/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/lib/site-packages/transcrypt/modules/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/lib/site-packages/transcrypt/modules/__javascript__/_socket.mod.js
                C:/Program Files (x86)/Python36-32/Scripts/__javascript__/_socket.mod.js
                C:/Program Files (x86)/Python36-32/Scripts/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/python36.zip/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/python36.zip/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/DLLs/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/DLLs/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/lib/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/lib/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/lib/site-packages/__javascript__/_socket.mod.js
                c:/program files (x86)/python36-32/lib/site-packages/__javascript__/_socket.mod.js

Aborted

Python File:

class Test:
    def __init__(self):
        print("HI")
        try:
            import socket
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except Exception as ex:
            print("Error: " + str(ex))

Quphoria avatar Jan 31 '17 15:01 Quphoria

Hi,

Some tips:

  • Transcrypt is a compiler rather than an interpreter. This means that imports are done compile time, to be able to compile also the imported modules and include them in the generated code. Since they are compile-time, they cannot be runtime conditional, generate runtime exceptions etc.. If you need compile time condional imports use __pragma__ ('ifdef', ... which works compile time.
  • You'll need websockets rather than the CPython sockets module, which is probably written in C, hence cannot run in a browser. Since you can use any JS library or API function, you can use e.g. HTML5 web sockets or any JS socket lib. Just use it exactly the same as from JavaScript.
  • In general, for Transcrypt, use JS libraries, since they're most suited to do their work in the browser.

See also: http://sterlicht.alwaysdata.net/transcrypt.org/docs/html/what_why.html#the-ecosystem-different-batteries

Hope this helps, otherwise let me know.

Kind regards Jacques de Hooge

JdeH avatar Jan 31 '17 16:01 JdeH

thx

Quphoria avatar Jan 31 '17 17:01 Quphoria

Since your question could also help others, could you repost on stackoverlflow under the [transcrypt] label.

http://stackoverflow.com/questions/tagged/transcrypt

No obligation of course, just if you find the time.

JdeH avatar Jan 31 '17 18:01 JdeH

Do you know any python libraries that i can use as websockets does not work as it requires sys

Quphoria avatar Feb 01 '17 10:02 Quphoria

You should use the Python websockets package only on the server side, so not with Trancrypt.

What I meant is that on the client you should use JavaScript websockets functionality as decribed a.o. at the following locations:

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

https://www.tutorialspoint.com/html5/html5_websocket.htm

http://www.developerfusion.com/article/143158/an-introduction-to-websockets/

Since these are JavaScript facilities, they won't require sys or things like that. So take care NOT to import websockets in your Transcrypt client code, since this will give you the Python package, rather than the JavaScript facility that is available natively.

BTW Thanks for posing your question on SO as well.

JdeH avatar Feb 01 '17 10:02 JdeH

thanks for the great support!

Quphoria avatar Feb 01 '17 13:02 Quphoria