IIS CGI could not locate my python modules - python

I am trying to run my python scripts on my IIS website using CGI and I am having trouble with importing. When run on its own, the python scripts finds the mysql.connector module installed in my os perfectly fine, but when I try to run it on the website, it gives a Bad Gateway (502.2) error with the stacktrace stating ModuleNotFoundError: No module named 'mysql.connector'. I'm assuming CGI cannot find the module in my OS, how can I let it find the module? Do I have to specify my modules folder somewhere in the IIS like a PATH variable?
Here is the complete stacktrace of the bad gateway page:
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "Traceback (most recent call last): File "C:\Users\pedro\OneDrive\Documents\adet\ind.py", line 2, in import python_mysql File "C:\Users\pedro\OneDrive\Documents\adet\python_mysql.py", line 1, in import mysql.connector ModuleNotFoundError: No module named 'mysql.connector' ".

As a workaround, I simply copied and pasted all of my modules in my modules folder to my website's folder. I guessed that since my personal imports were working that it should also work if I added the other modules to the same folder and lo, it did. I hope there's a cleaner way to solve this, but for now I'll choose this as a solution.

Related

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

Python Import Error when working with GoogleAppEngine

I have Google AppEngine SDK installed(at /usr/local/google-appengine), and the toy App can be launched and run with GoogleAppEngine Launcher with no issue.
However, I have some standalone scripts (testpbuf.py) in the app folder that I want to run with
$python testpbuf.py
then I got Python Import errors:
Traceback (most recent call last):
File "testpbuf.py", line 3, in <module>
from google.appengine.api import files
ImportError: No module named appengine.api
The script is trying to import AppEngine API and protorpc modules..
What's going one here? I have the SDK included in my PATH and I have no problem invoking from terminal. Any insights? Thanks a lot!
You need to get all the libraries inside the SDK added to your path. This can be done as follows:
import dev_appserver
dev_appserver.fix_sys_path()
Running code outside of the SDK web-server for things other than tests isn't likely to be that useful to you though ... when it runs on appengine, it has to be via WSGI.

Python Code unable to find dependencies with launchd plist Mac OS 10.8.5

I have a python script which scrapes a website for information every 5 minutes all for personal use. The script works fine when executed from the terminal, however when it is executed by launchctl it gives an error as if the installed python module cannot be found:
Traceback (most recent call last):
File "/Library/Application Support/PythonDaemons/TrafficScraper.py", line 36, in <module>
from bs4 import BeautifulSoup
ImportError: No module named bs4
I know there are some special considerations because the system is executing it not the user however I don't know where to start on fixing it. Thanks for the help.
I added the PYTHONPATH environment variable to the plist and the application worked.

Import and python source file in folders

I have some troubles understanding how python import works.
I just forked this repository and I am going to play with it but i am having troubles understanding how python import works with file in directories.
The directory structure is the following:
fitbit/
__init__.py
gather_keys_cli.py
api.py
exceptions.py
utils.py
As a preliminary step I need to run gather_keys_cli.py. It depends on api.py, which in turn depends on exceptions.py.
If I try to run it from the python interpreter everything works as expected. If I try to run it from the command line (As suggested in the documentation) I have obtain the following exception.
$./fitbit/gather_keys_cli.py KEY SECRET
Traceback (most recent call last):
File "./fitbit/gather_keys_cli.py", line 34, in <module>
from api import FitbitOauthClient
File "/Users/mariosangiorgio/FitBitHacks/python-fitbit/fitbit/api.py", line 9, in <module>
from fitbit.exceptions import (BadResponse, DeleteError, HTTPBadRequest,
ImportError: No module named fitbit.exceptions
My understanding is that when I invoke the command line the base path is set to the folder containing it. At this point, the scripts correctly imports the oauth client because it uses this instruction from api import FitbitOauthClient, which does not contain any reference to the fitbit directory.
On the opposite, api.py uses this instruction to import exceptions from fitbit.exceptions import (LIST_OF_CLASSES). This instruction contains the reference to the fitbit directory and hence I got the error.
I am not familiar with python so I'd like to understand what is the pythonic way to solve this issue.
I have the feeling that there is something clearly wrong going on and I found a workaround by moving gather_keys_cli.py and changing the api import to import fitbit.api. It works but I am not sure it is the right way to solve the issue I'm experiencing.

Jython 2.5.1: "ImportError: No Module named os"

I looked through the other posts and bug reports and couldn't figure out what's causing this. I'm using Jython 2.5.1, in a Java project in Eclipse (Ubuntu 8.10). It has been added to the project as a standalone .jar file (I just replaced the old Jython 2.1 jar with this one).
I'm running a script that uses the threading.py class. At some point the statement "import os" is evaluated from linecache.py and I get this error, which I can't seem to figure out how to fix:
'Execution failed. Traceback (most recent call last):
File "<string>", line 1, in <module>
File "../lib/python/threading.py", line 6, in <module>
import traceback
File "../lib/python/traceback.py", line 3, in <module>
import linecache
File "../lib/python/linecache.py", line 9, in <module>
import os
ImportError: No module named os'
What do you mean with "the jar that comes with the 2.5 download"? Did you extract the contents and use jython.jar or did you run the installer? If you just extracted and didn't run the installer your jython.jar will miss the whole LIB folder.
Can you check if jython.jar contains a LIB folder? (e.g. open jython.jar with 7z or WinZip).
Or try copying the LIB folder in the same folder where jython.jar resides.
Did you try setting these properties. Jython Registry. e.g. via -Dpython.home in the eclipse run configuration.
python.cachedir
python.path
python.home
How is the jar named? If similar to jython-complete.jar try renaming it to jython.jar
Something is wrong at a very deep level, but it's probably easy to fix. You are seeing an error that happens while trying to report some other error.
Probably you have your PYTHONPATH misconfigured. I don't know the details of Jython or Eclipse running Jython, but it looks like you have no standard library available to you.
If you are getting maven, using the dependency jython-standalone instead of jython may help (at least it did for me in a maven project with jython-standalone-2.5.3)

Categories