Import makes Python-CGI crash (error 500) - python

Being script1.py:
import cgi
import cgitb
print("Content-type:text/html\r\n\r\n")
print("hello")
and script2.py:
import cgi
import cgitb
import pymysql
print("Content-type:text/html\r\n\r\n")
print("hello")
In script1.py everything works fine (prints 'hello'), but script2.py returns a 500 error.
Considerations:
pymysql is installed and the proof is that it can be imported from the python interactive shell without trhowing any exception
both files have the same permissions
Then, why script2.py doesn't work?

You might consider if your script is being run by the web server (as opposed to your user shell session) in a different virtual environment, possibly one that doesn't have pymysql installed. If that's the case, you'll need to activate that virtual environment and install pymysql there.
To verify this is an instance of the library not being installed, you might try something like this in your script...
import cgi
import cgitb
import sys
print("Content-type:text/html\r\n\r\n")
print("hello")
if 'pymysql' in sys.modules:
print 'pymysql is installed'
else:
print 'pymysql is NOT installed'
Beyond this, checking your web server logs might provide more insight into why the 500 error is getting raised by the web server...

Related

How to import the google api client into python so that it runs with as a cronjob?

I am trying to use the google API client to download something every day for something but I have deduced that for some reason it won't let me import googleapiclient when I run it in crontab.
For example, if I run this in crontab
crontab: * * * * * python3 test.py >> cron.log
test.py:
print("Hello")
cron.log outputs:
Hello
But if I then once I import it so the file look like this
from googleapiclient import discovery
print("Hello")
then the cron.log looks like this:
Its just empty. I can not figure out why this is true. The google API client I believe is installed correctly because when I run it manually then it works perfectly with no issues.
The operating system I am using is macos.
The way I fixed it is first by changing the crontab to python3 test.py > cron.log 2>&1. This showed that the errors where. Then I realized that it was not able to import googleapiclient into python. I still did not understand why it does not, but a workaround I found was to first install the libraries into a folder and then accessing the libraries from there.
To install the libraries into a folder the command I used in terminal is below:
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib -t ./lib
Then inside the python I added whats below to the beginning of the file so it would know where to import the libraries from.
import os
import sys
file_path = os.path.dirname(__file__)
module_path = os.path.join(file_path, "lib")
sys.path.append(module_path)
This does work.

Python with iis import modules

I have a working python 3.7.3 (Anaconda) and configured iis 10 on windows 10
I address the following script via http://127.0.0.1/py3/hw.py
This works without error so modules can be imported
import sys
print('Content-Type: text/html')
print('')
print('Hello, world! ')
but the following does not work using http://127.0.0.1/py3/hw.py, claiming that the module numpy can't be located
though running it from the Anaconda command prompt does work
import sys
import numpy
print('Content-Type: text/html')
print('')
print('Hello, world! ')
I presume there is a path issue here
I have tried adding the Anaconda path additions to the standard windows path which does not work
Any suggestions most welcome
I made some test on my local and it is working fine.
1.I installed numpy via command line: pip install numpy==1.17.3
2.Please ensure your authenticated user like NT Authority/IUSR have enough permission to access your python's lib folder.
It is recommended to troubleshooting this issue with Microsoft Process monitor and scan access denied error inside w3wp.exe or python.exe.
https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

Can't import some existing modules in Python

I'm using Python on my web server (which is Heliohost) and I'm unable to import some Python modules. Whenever I try to import them, I get a 500 internal server error.
The modules I'm not able to import are numpy and tensorflow, which should be installed because running the script below that should print all the installed modules
#!/usr/bin/python3.6
import cgitb
import pip
print("Content-Type: text/html")
cgitb.enable(format='text')
installed_packages = pip.get_installed_distributions()
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
for i in installed_packages])
returns a lot of Python modules, with tensorflow and numpy as well.
Basically, the scripts work fine with any other Python module, it gives an error only with those two.
What could be the problem? Unfortunately, I can't access the server logs because my web server provider doesn't allow it, and with cgitb enabled (that should print any error in the code), it just returns the error 500 without printing anything else.

how to change python version used by apache?

When I launch my CGI (common gateway interface) by using this code:
# -*- coding: utf-8 -*-
import cgi
import os
import json
import cgitb
cgitb.enable()
from sklearn.feature_extraction.text import CountVectorizer
print 'Content-type: text/html'
print
print '<html><head><title>Interface Ticket Recognition</title></head><body>'
formulaire = cgi.FieldStorage()
os.chdir("C:\Users\iyacine\Desktop")
path= os.getcwd()
from subprocess import call
call(["Python", "applicationV1.py"])
if path=="C:\Users\iyacine\Desktop":
print '''changer'''
print '</body></html>'
I got an error:
I try to change the path in environment variable but this work only in cmd.
I installed anaconda i my computer and i can run the module sklearn.feature_extraction.text in Spyder but not from Apache. Do you know how to solve this issue please?
You should have a first line like #! /usr/bin/python (called Shebang line) in your script. You have to change that line to point to the python executable you actually want. For Windows have a look at the docs but there also is the possibility to run ".bat" files via CGI so you could use a file (eg runmyscript.bat) containing just
#C:\path\to\anaconda\python.exe path\to\script.py
which is a hacky workaround (I think) but should work, too.
In my experience it is the best to use virtualenv if possible - this does not fix the path-problem directly but helps keeping different applications isolated.

No module named ib.message

I am trying to set up IBPY and Python on my Mac to run with Interactive Brokers. I have installed Git. I created a subdirectory ibapi under Home. I downloaded IBPy using git clone https://github.com/blampe/IbPy from the ibapi directory.
I am now trying to run the Demo.py program in Spyder. When I choose Run, I receive the error message:
ImportError: No module named Ib.Message
The first few lines of the demo program are:
import os
import sys
import time
import Ib.Message
import Ib.Socket
import Ib.Type
I am also trying to run a sample program: ib_api_demo from http://www.quantstart.com/articles/Using-Python-IBPy-and-the-Interactive-Brokers-API-to-Automate-Trades. When I try to run this, I get the error message:
ImportError: No module named ib.ext.Contract
The first few lines are:
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import Connection, message
I figured this out. The problem was I was launching Spyder from my Mac Finder. When I did this, I received the error messages. When I launched Spyder (actually Anaconda Python) by typing "Spyder" in the Terminal window, this launched Anaconda Python. From here, I could run all my programs successfully with no errors.

Categories

Resources