How can I import files when running a CGI script? - cgi

I have an HTML form that submits a POST request to cgi-bin/output.py. I want to extract the data from the form and then manipulate it using imported functions from other files.
This is on Windows command prompt, running Python 3.7 and using the CGI module. I have followed instruction on other posts in regard to how to import files (_init_py, using importlib.util) but I think this may be a CGI specific problem.
<!--index.html-->
<form class="formBox" action="cgi-bin/output.py" method="post">
<select name="dropdown" autofocus/>
#output.py
import cgi, cgitb
import tagging
from urllib.request import urlopen
form = cgi.FieldStorage()
dropdown = form.getvalue('dropdown')
tagging.tagger(dropdown) #call function from different file on argument from HTML form
The tagging.py file is in cgi-bin along with output.py. I want to be able to use a function from tagging.py but when I try to import the file I get a ModuleNotFoundError.

Have you tried to run it? If both files are in the same directory, it should work.
I just tested it on Windows 10, and it works as expected.

Related

Google Colab - Not able to import API key file

I have a code in Google Colab which uses a Python package called Atlite, which in turn retrieves data from the Climate Data Store (CDS) through the use of an API key.
When running this code in Python I just need to have the file containing the key saved in a specific folder and then the code runs perfectly fine.
When I try to run the code in Google Colab the following error arises:
**Exception: Missing/incomplete configuration file: /root/.cdsapirc
**
I have the file ".cdsapirc" in my computer but when I try to import it to the "/root" folder in Google Colab it just does not get imported. I can import a .py file, but when I try to import the ".cdsapirc" file (which is basically a txt file) it does not work.
Could someone please help me to solve this issue?
Thank you!
Regards,
Sebastian
If uploading the .cdsapirc file doesn't work, you could try creating it inside Google Colab using a simple Python script:
uid = "<your uid>"
apikey = "<your api-key"
with open("/root/.cdsapirc", "w") as f:
print("url: https://cds.climate.copernicus.eu/api/v2", file=f)
print(f"key: {uid}:{apikey}", file=f)
You can get the uid and apikey either from CDS after logging in or you open you local .cdsapirc file and look them up there, see here for more information.
There might be a nicer solution by someone more familiar with Google Colab though.

Installed modules not found, but show up in python interpreter config

I tried to install EZGmail module for Python. When I check my interpreter settings, it shows it as installed in the PyCharm list. I then use this same interpreter for my project, but I get a module not found error when trying to import EZGmail. What should I check?
Looks like you're trying to import the module in a different casing than it's mentioned on the document.
You're trying to do:
import EZGmail
while the Quickstart document says:
import ezgmail

is there a Way to Grab the Filename and location from a File that is being downloaded by selenium and webdriver

I have a script that automates downloading a file using selenium and webdriver as chrome
basically, it logs in to a work website, and clicks on some settings to prepare to download a report file, and then clicks the download button
Is there a way for selenium or any other library to grab the file location and name of that file that is downloading or has just downloaded so I can store it in a variable to use later in the script
I do not know if the relative path will work or if it needs to be a full windows path name for it to work... probably safe to assume full path is more guaranteed to work
example being C:\Users\FunnyUserName\Downloads\report.xls
Adding Code to show what is happening better
#For Report Pull
#-----------------------------------------------
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
import os
import glob
###############################################################
#Pull Report #
###############################################################
#Open Web Driver
browser = webdriver.Chrome()
#Open Website and Log in
print('Open Website and Log In')
browser.get(('https://SomeWebsite.com'))
print('Working on Getting the QC Report')
print('Please Stand By')
#####
#I Removed a lot of stuff not necessary to this question
#Get the File
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="btnGenerateReport"]'))).click()
time.sleep(4)
#Working on getting the last downloaded Filename
# get the user download folder (dynamic so will work on any machine)
downLoadFolder =os.path.join( os.getenv('USERPROFILE'), 'Downloads')
print(downLoadFolder)
#This shows the correct folder....
#In My Case C:\Users\My UserName\Downloads
# get the list of files
list_of_files = glob.glob(downLoadFolder+"/*.*") # * means all if need specific formats (if you are looking for any specific format then specify eg: "/*.xls" to filter)
print (list_of_files)
#Always Shows ['C:\\Users\\My UserName\\Downloads\\desktop.ini']
# get the latest file name
#Forced the Folder and file type as a test
latest_file = max(glob.glob("C:/Users/My Username/Downloads/*.xls"), key=os.path.getctime)
#print the latest file name
print(latest_file)
#Returns:latest_file = max(glob.glob("C:/Users/My Username/Downloads/*.xls"), key=os.path.getctime)
#ValueError: max() arg is an empty sequence
I might have figured out where the problem is
I need it to grab the actual downloads directory, not the system default
I'm testing on my home computer, it default downloads to my dropbox download directory that I share with my work computer as well
so its not C:\Users\My Username\Downloads\
its actually D:\Dropbox\Downloads... which is where my chrome default is set currently
how do I get the chrome download directory?
Here is the solution in the python.
Imports Needed:
import glob
import os
Script:
# get the user download folder (dynamic so will work on any machine)
downLoadFolder =os.path.join( os.getenv('USERPROFILE'), 'Downloads')
# get the list of files
list_of_files = glob.glob(downLoadFolder+"/*") # * means all if need specific formats (if you are looking for any specific format then specify eg: "/*.xlsx" to filter)
# get the latest file name
latest_file = max(list_of_files, key=os.path.getctime)
#print the latest file name
print(latest_file)

Import another python script, using PythonInterpreter

I am trying to execute a python method from eclipse using jython. I managed to run it with following code:
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("Mypython.py");
interpreter.eval("MyClassName().MyMethodName()")
My problem is when I import another python script, which exists even in the same directory with Mypython.py. For example, when I add:
from food import Pizza
to Mypython.py, it starts to complain that cannot import. ImportError..
I found some questions about importing python libaries like os, but in my case this is not an issue.
I tried to make the folder as a package, add init.py etc but it failed. I saw some people use PySystemState, but I think it is for jython modules not user python scripts. if this is the solution please give me a simple example.
Could you please help me with that problem.
sys.path is your module-import search path. You can import sys and then modify sys.path as required.

Py2exe doesn't find bs4

In my original code, I have the line:
from bs4 import BeautifulSoup
When I use py2exe, it builds fine but further up in the output it says:
The following modules appear to be missing
['_scproxy', 'bs4']
I specifically put bs4 in the py2exe options:
"includes": ["bs4.BeautifulSoup"]
Is that how I should be referencing BeautifulSoup in the includes statement?
The fella over here didn't know how to do it either: 3rd Party Libraries and Py2exe
Do I need to use packages instead of includes or something? All regular libraries and some other like mechanize import fine, but I can't get BeautifulSoup to work fine. Any advice is appreciated.
EDIT: I solved part of this by uninstall BeautifulSoup and re-installing with --always-unzip option:
easy_install --always-unzip beautifulsoup4
However, it added 9 new missing modules. One being '_scproxy'.
First thing to check is that you have your setup.py in the same directory as your module and your are running it from that directory.
If that doesn't work your should add your module to your path in setup.py:
module_path = r'path\to\your\BeautifulSoup\module'
if module_path not in sys.path:
sys.path.append(modules_path)