Python Forum
Trying to understand how import works in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trying to understand how import works in python (/thread-10877.html)



Trying to understand how import works in python - patrick99e99 - Jun-11-2018

I am using a python application which requires SimpleWebsocketServer..

I have done:

Quote:python3 -m pip install git+https://github.com/ecdsa/simple-websocket-server.git

Collecting git+https://github.com/ecdsa/simple-websocket-server.git
Cloning https://github.com/ecdsa/simple-websocket-server.git to /private/var/folders/3d/n3z5tt2n3fqc186ngh1ysmfr0000gn/T/pip-54tadi56-build
Installing collected packages: SimpleWebSocketServer
Running setup.py install for SimpleWebSocketServer ... done
Successfully installed SimpleWebSocketServer-0.1.0

But when I run the app, it fails from Import Error:
https://github.com/fyookball/electrum/blob/master/lib/websockets.py#L32

...

When I go into the python3 console, I can do:
Quote:Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import SimpleWebSocketServer
>>> SimpleWebSocketServer
<module 'SimpleWebSocketServer' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/SimpleWebSocketServer/__init__.py'>

So it looks like it is there.......... But if I try to do exactly what the app is doing, I have the same error:

Quote:>>> from SimpleWebSocketServer import WebSocket, SimpleSSLWebSocketServer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'WebSocket'

Can anyone help with what I am doing wrong?

Thanks!


RE: Trying to understand how import works in python - snippsat - Jun-11-2018

This is a example of how websocket work.
It's not the meaning that you should import,should run the server from command line.
λ python SimpleExampleServer.py --example echo
Now is the server running,open websocket.html.
There is a connect button.
Then send and get a response.
Output:
connected sent: hello world response: hello world



RE: Trying to understand how import works in python - patrick99e99 - Jun-11-2018

Thanks but that's not at all related to the question I asked....


RE: Trying to understand how import works in python - patrick99e99 - Jun-12-2018

Nevermind, I found that the problem was the code... Using the parent repo instead of the fork I was using resolved the issue.. this thread can be deleted.