Python KeyError while running script - python-3.3

I am running the below python script.
import cgi
import os
import time
import sys
import yate
print(yate.start_response('text/plain'))
addr=os.environ['REMOTE_ADDR']
host=os.environ['REMOTE_HOST']
method=os.environ['REQUEST_METHOD']
cur_time=time.asctime(time.localtime())
print(host+","+addr+","+cur_time+":"+method+":",end='',file=sys.stderr)
and i am getting the below error.
addr=os.environ['REMOTE_ADDR']
File "C:\Python33\lib\os.py", line 676, in __getitem__
raise KeyError(key) from None
KeyError: 'REMOTE_ADDR'
Please help on this....

Your script is supposed to be run as a CGI script by a web-server, which sets environment variables like REMOTE_ADDR, REQUEST_METHOD, etc.
You are running the script by yourself, and these environment variables are not available. That's why you get the KeyError.

Related

ERROR: Traceback (most recent call last) when running a python script from tcl

I am running a python script that is called from tcl with the exec command. I have a bunch of modules that are being imported in the py script.
When I run this, I get the error "ERROR: Traceback (most recent call last)"
I noticed that it happens only when numpy and paramiko modules are imported. It seems to run just fine without these two leins.
I am not sure what the backend process is when other scripts are called within tcl. Any info is appreciated.
Here is the example of what my file has.
tcl:
exec python hello.py
hello.py:
from pathlib import Path
#import numpy
import subprocess
#import paramiko
import logging
import itertools
Like I said, it works with the commented out import statements.
Edit: The tcl interpreter is embedded in another software I am using (IPG CarMaker). I am guessing that this may be the issue.

Cannont run PyGlossary on Windows10

Absolute noob here, so I apologize in advance for stupid questions.
I'm running Windows 10, I've got Python 3.7.3 and I have...
Successfully installed pyglossary-3.2.1
But I cannot run PyGlossary from the command line or the tkinter GUI. If I run any of the following lines
python3 pyglossary.pyw --help
python3 pyglossary.pyw --ui=tk
python3 pyglossary.pyw --version
Nothing happens and I don't get any sort of error message. I've also tried...
python pyglossary.pyw --help
and I get...
Traceback (most recent call last):
File "pyglossary.pyw", line 30, in
from pyglossary import core # essential
File "C:\mypath\pyglossary\pyglossary.pyw", line 30, in
from pyglossary import core # essential
ImportError: cannot import name 'core' from 'pyglossary' (C:\mypath\pyglossary\pyglossary.pyw)
I've checked Windows Path variables, but I think everything's ok there, and I'm able to run other python stuff. Thanks in advance.

No module named psutil when executed using python filename.py but works perfectly with shebang & chmod

consider this code in python
#!/usr/bin/env python3
import shutil
import psutil
def check_disk_usage(disk):
a=shutil.disk_usage(disk)
free=a.free/a.total*100
return free
print(check_disk_usage("/"))
when i execute this in terminal using "python health.py". it gives error
shambhav#shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ code health.py
shambhav#shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ python health.py
Traceback (most recent call last):
File "health.py", line 3, in <module>
import psutil
ImportError: No module named psutil
but when i execute like this it works perfectly. why so??
shambhav#shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ code health.py
shambhav#shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ chmod +x health.py
shambhav#shambhav-Lenovo-Legion-Y540-15IRH-PG0:~/Desktop/coursera$ ./health.py
87.24079278480475
Can someone help me with this??
In your shebang, you explicitly use python3, but not when you launch it from the terminal. Hence, try:
python3 health.py
In order to understand, try typing which python or python --version in your terminal. I bet it is not pointing to python3, but python2.

Unable to run python code from Linux command line

I have gone through the explanations given in this forum and have tried them in my program. However, none of the suggestions worked. That's why I am opening this thread.
Below is the tree for my project. There are 2 packages: com and main.
When I try to run the code for ProcessRiskModelbyRecordID.py from command line, I am getting below error message:
$ python3 /AppDev/XXXX/py/riskScore/main/ProcessRiskModelbyRecordID.py
Traceback (most recent call last):
File "/AppDev/XXXX/py/riskScore/main/ProcessRiskModelbyRecordID.py", line 6, in
from main.ConnectAPI import *
ModuleNotFoundError: No module named 'main'
When I run the same code from PyDev, I am able to execute it.
Below is the import code from ProcessRiskModelbyRecordID.py:
from main.ConnectAPI import *
from com import DBOperations as DBO,SourceProfile,TargetProfile
Can you please help so that I can run this code from command line?
PyDev is probably setting PYTHONPATH for you. On the command line you would need to set it yourself:
cd riskScore
export PYTHONPATH=`pwd`
python3 main/ProcessRiskModelbyRecordID.py

Using py2exe by just running the script, no command-line

I know there's already a question like this but the answer didn't work for me.
When I append the py2exe argument in the program, and run it, I noticed it's missing a bunch of files. Also when I run the exe, it says
File "boot_common.py", line 46 in <module>
ImportError: No module named 'ctypes'
Can anyone help me on how to fix this? Here's my code:
from distutils.core import setup
import py2exe, sys
import tkinter as tk
from tkinter import filedialog
input('Press Enter to continue and select your Python file you want to convert when the dialog shows up...')
tk.Tk().withdraw()
file_path = tk.filedialog.askopenfilename()
sys.argv.append("py2exe")
setup(console=[file_path])
Also if it helps, I'm using a 32-bit Python interpreter.

Resources