I am trying to get Python code to work on a web browser using the CGI module, as well as some other file modules that I am trying to import into the main program.
My initial program, which works as required, is the following:
#!C:\Users\Student\AppData\Local\Programs\Python\Python37-32\python.exe
import cgi
def htmlTop():
print("Content-type: text/html")
print()
print("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Server Side Test</title>
</head>
<body>""")
def htmlTail():
print("""</body>
</html>""")
if __name__ == "__main__":
try:
htmlTop()
print("Hello World")
htmlTail()
except:
cgi.print_exception()
In the same folder as this program, I have a Python file called "_serverTestModules" containing:
def _serverTestFunction():
print("The file has been imported successfully")
If I then add:
import _serverTestModules
to the original file, both of which are in the same directory, and attempt to access the function from the original code, I get this error:
Traceback (most recent call last):
File "C:/xampp/htdocs/inventorysystem/_serverTest.py", line 26, in <module>
_serverTestFunction()
NameError: name '_serverTestFunction' is not defined
So basically my test program works, but as soon as I try to import functions from other files which are in the same directory (something which I need for my main project), the code fails. Is this a problem with the computer system or the network? Every other answer I have seen on Stack Overflow has either had no answer or an incorrect answer, so any help is much appreciated.
You need to use this
from _serverTestModules import _serverTestFunction
Related
follow by discussion Get output of python script from within python script
I create online webpage (.html) run other .py script result, but face model not found error
logically seems the python code is fine, might the setting error or other issue I don't come up myself
printbob.py
#!/usr/bin/env python
import sys
def main(args):
for arg in args:
print(arg)
if __name__ == '__main__':
main(sys.argv)
test_0109_003.html
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<b><p>title test 1.10-test_get_ print </p></b>
<br>
<py-script>
import printbob
printbob.main('arg1 arg2 arg3 arg4'.split(' '))
</py-script>
</body>
</html>
(pic 01) the result website showing
(pic 02) I put .py and .html script in WinSCP, online host system
so how to solve this problem, I locate that there might be the resaon
my winscp ip port is private that public cannot access to private's file, I'm not sure if this the reason, and if so, how to deal with it?
I'm trying to use Eel to make a simple application, but when I run this simple code:
import eel
from os import getcwd
eel.init("web")
eel.start(getcwd() + "\\main.html")
It gives me this error:
If it's important, here is the html code, just a placeholder to test:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<body>
<h1>Test</h1>
</body>
</html>
I've tried changing ownership of the file to 'Everyone' and giving full access, but that didn't do anything. I couldn't find anything about this online when I searched for it, so if anyone knows how to fix this, please let me know. TIA!
This app.py worked for me:
import eel
if __name__ == '__main__':
eel.init(".")
eel.start("main.html")
With my directory structure:
/project
|app.py
|main.html
With the code you posted, you would need a directory structure like:
/project
|app.py
/web
|main.html
I just restarted my vs code, and it's worked for me.
I am trying to import the PSAW library in python using Brython, I have the following code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document
document <= "Hello world !"
</script>
</body>
</html>
This works correctly, however when i add PSAW like so:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="brython.js"></script>
<script type="text/javascript" src="brython_stdlib.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document
import PushshiftAPI
document <= "Hello world !"
</script>
</body>
</html>
I get the following error in chrome:
GET http://localhost:8000/requests.py?v=1571453029466 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/requests.py
GET http://localhost:8000/requests/__init__.py?v=1571453029904 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/requests/__init__.py
GET http://localhost:8000/Lib/site-packages/requests.py?v=1571453030155 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/Lib/site-packages/requests.py
GET http://localhost:8000/Lib/site-packages/requests/__init__.py?v=1571453030212 404 (File not found)
Error 404 means that Python module requests was not found at url http://localhost:8000/Lib/site-packages/requests/__init__.py
Traceback (most recent call last):
ImportError: No module named requests
I think it means it requires other modules with Brython can't find but I'm not sure how to identify what modules are needed and how to correctly add them. Here is my file directory:
Update:
I have installed PSAW and PRAW in a virtual environment and copied all packages into the directory like so:
However I still receive the following issue when trying to import requests
Traceback (most recent call last):
AttributeError: 'NoneType' object has no attribute 'load_module'
Uncaught Error
at Object._b_.AttributeError.$factory (eval at $make_exc (brython.js:7609), <anonymous>:41:354)
at attr_error (brython.js:6587)
at Object.$B.$getattr (brython.js:6701)
at Object.import_hooks (brython.js:13361)
at Object.$B.$__import__ (brython.js:9097)
at __import__ (brython.js:6761)
at _import_module179 (eval at run_py (brython.js:8906), <anonymous>:238:45)
at _resolve183 (eval at run_py (brython.js:8906), <anonymous>:539:84)
at method (brython.js:5321)
at load_module195 (eval at run_py (brython.js:8906), <anonymous>:1377:148)
Any idea what's happening here?
A 404 error means "file not found." Your browser is trying to access a library located at "http://localhost:8000/requests/__init__.py?v=1571453029904" (and the other locations in your traceback) but can't find it. Try visiting the URL in your browser. Does it return a file? If you can't find a file there, Brython won't be able to either. Try rearranging your filesystem until your can visit the .py file in a browser
I'm running Windows 7, and using Google Chrome as my web browser.
This is my code:
#!/usr/bin/env python
print 'content-type: text/html\n'
print """
<!DOCTYPE html>
<html>
<head>
<title> sample </title>
</head>
<body>
"""
print 2 + 3
print """
</body>
</html>
"""
In Command Prompt, where python results in C:\Python27\python.exe, but according to this post I should use #!/usr/bin/env python instead.
When I try to run this .py code in my browser, the source code appears instead of just 5.
If someone would provide detailed instructions as to how to properly work this, I would be most grateful.
Thanks!
Okay. So of course it is not possible to run python directly in the browser but you can configure a local server to be able to run CGI scripts. Since you are on windows you could use something like a WAMP.
Try following this tutorial
https://wiki.python.org/moin/CgiScripts
I'm getting an internal server error (500 err) "End of script output before headers" trying to run a cgi using
XAMPP Apache on Windows
Python 3.3
Notepad++ with UNIX Style (\n) newline chars
My script reads as follows
#!"C:\Python33\python.exe"
import cgi
def htmlTop():
print("Content-type: text/html")
print()
print("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Server Side Test</title>
</head>
<body>""")
def htmlTail():
print("""</body>
</html>""")
if ___name___ == "__main__":
try:
htmlTop()
print("Hello World")
htmlTail()
except:
cgi.print_exception()
Please note I have tried using print("Content-type: text/html\n\n") as opposed to the extra print statement. Thanks!
I know this is an old post but I found the errors in your script
The first mistake I found was the quote you used when requiring python, so
#!"C:\Python33\python.exe" should be changed to #!C:\Python33\python.exe
The second mistake I found was the additional bars you used There are 3 bars here->___name___<-and here so
___name___ should be changed to __name__
So the final code should be
#!C:\Python33\python.exe
import cgi
def htmlTop():
print("Content-type: text/html")
print()
print("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>My Server Side Test</title>
</head>
<body>""")
def htmlTail():
print("""</body>
</html>""")
if __name__ == "__main__":
try:
htmlTop()
print("Hello World")
htmlTail()
except:
cgi.print_exception()