PythonKit for OSX Swift project suddenly not finding modules - python

I built a MacOS app using PythonKit to run some scripts and return the data. I had it running with no issues the other day but now it crashes when trying to load the script file. I've verfied the path to the file is correct and have run the Python script in Terminal with no issues.
Here's the error message:
Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'helloswift': file PythonKit/Python.swift, line 674
2021-04-09 10:01:55.441796-0400 TechCommToolKit[10754:2952424] Fatal error: 'try!' expression unexpectedly raised an error: Python exception: No module named 'helloswift': file PythonKit/Python.swift, line 674
The line of code in PythonKit throwing it:
public func `import`(_ name: String) -> PythonObject { return try! attemptImport(name) }
My Python code in the file:
import requests
import json
import sys
def getConfluenceData(strEndPoint):
s = requests.Session()
response = s.post('https://wiki.xxxx.com/dologin.action', data={'os_username': 'app_wiki_api2', 'os_password': 'xxxxxxxxxxxxxxxx'})
response = s.get(strEndPoint)
json_formatted_str = json.dumps(response.json(), indent=4)
return(json_formatted_str)

Ug, this was operator error, I was pointing to the file, not the directory containing the file.

Related

Call speedtest.Speedtest() from Python using --secure (to avoid speedtest.ConfigRetrievalError: HTTP Error 403: Forbidden)

I have a small Python3-script like this:
import speedtest
# Speedtest
test = speedtest.Speedtest() # <--- line 4
test.get_servers()
best = test.get_best_server()
print(f"Found: {best['host']} located in {best['country']}")
The first time I run it, it works and everything is fine; it outputs:
Found: speedtest.witcom.cloud:8080 located in Germany
Happy days.
The second time (and subsequel times) that I run the script, I get this error:
Traceback (most recent call last):
File "/Users/zeth/Code/pinger/pinger.py", line 4, in <module>
test = speedtest.Speedtest()
File "/usr/local/lib/python3.9/site-packages/speedtest.py", line 1095, in __init__
self.get_config()
File "/usr/local/lib/python3.9/site-packages/speedtest.py", line 1127, in get_config
raise ConfigRetrievalError(e)
speedtest.ConfigRetrievalError: HTTP Error 403: Forbidden
When Googling around, I saw that I could also call this module straight from the command line, but just running this:
$ speedtest-cli
That gives me the same kind of error:
Retrieving speedtest.net configuration...
Cannot retrieve speedtest configuration
ERROR: HTTP Error 403: Forbidden
But if I run the direct cli-command: speedtest-cli --secure ( docs for the --secure-flag ), then it goes through and outputs this:
Retrieving speedtest.net configuration...
Testing from Deutsche Telekom AG (212.185.228.168)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by hotspot.koeln (Cologne) [3.44 km]: 28.805 ms
Testing download speed................................................................................
Download: 30.01 Mbit/s
Testing upload speed......................................................................................................
Upload: 8.68 Mbit/s
The question
I can't figure out how to change this Python-line: test = speedtest.Speedtest() to use a --secure-flag (nor via HTTPS).
The documentation for speedtest-cli is scarce.
Other attempts
I found this solution here: Python Speedtest facing problems with certification _ssl.c:1056, that suggests manually approving the certificates.
But in this directory: /Volumes/Macintosh HD/Applications/ I don't have anything called Python3.9. I have python3.9 installed via Brew. And I'm on a Mac.
I could do this:
test = speedtest.Speedtest(secure=True)
I looked into the source code myself, in this directory:
vim /usr/local/lib/python3.9/site-packages/speedtest.py
Where I would see the function was defined like this:
class Speedtest(object):
"""Class for performing standard speedtest.net testing operations"""
def __init__(self, config=None, source_address=None, timeout=10,
secure=False, shutdown_event=None):
self.config = {}
self._source_address = source_address
self._timeout = timeout
self._opener = build_opener(source_address, timeout)
self._secure = secure
...
...
...

Why can't I import without getting an error about another python file? ("partially initialized module has no attribute")

I'm trying to import the requests module to get familiar with bs4, but the request module in the file I'm currently working in is grayed out so it isn't being recognized as a module. When I run the almost empty program, I get an error for an unrelated python file within my project.
Should I individually store each python file I make inside of a separate folder?
Both of these files are inside of the same project folder.
import requests
response = get('https://www.newegg.ca/p/N82E16868105274')
print(response.raise_for_status())
Error:
Traceback (most recent call last):
File "C:\Users\Denze\MyPythonScripts\Webscraping learning\beautifulsoup tests.py", line 1, in <module>
import requests
File "C:\Users\Denze\MyPythonScripts\requests.py", line 3, in <module>
res = requests.get('')
AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import)
Process finished with exit code 1
The other code in question that I think is causing my error:
import requests
res = requests.get('')
playFile = ('TestDownload.txt', 'wb')
for chunk in res.iter_content(100000):
playFile.write(chunk)
playFile.close()
You have a name collision. You're not importing the requests library, you're importing your script.
You wanted to do the following with your imports:
MyPythonScripts\beautifulsoup tests.py
→ requests.get() (the library)
What you're doing instead is:
MyPythonScripts\beautifulsoup tests.py
→ MyPythonScripts\requests.py
→ MyPythonScripts\requests.py .get() (the same file again)
That's the "circular import" that is mentioned in the traceback. The module imports itself and tries to use an attribute that isn't there before it finishes "executing", so the interpreter thinks it's due to the unfinished initialization
Raname MyPythonScripts\requests.py to something else and it should work.

The action didn't produce the valid JSON object in python file

I am using two actions of IBM cloud function - write1 and write2 (both using PYTHON).
I created a sequence that should pass value from write1 to write2.
I wrote a PYTHON code in write1 action but is throws some JSON error.
Write 1 Python File:*
import os
import sys
import json
import requests
import ibm_boto3
from ibm_botocore.client import Config, ClientError
cos = ibm_boto3.resource("s3",
ibm_api_key_id='my-api-key',
ibm_service_instance_id='my-instance-id',
config=Config(signature_version="oauth"),
endpoint_url='https://s3.eu-gb.cloud-object-storage.appdomain.cloud'
)
def get_item(bucket_name, item_name):
a={"Retrieving item from bucket":bucket_name , "key": item_name}
print(json.dumps(a))
try:
file = cos.Object(bucket_name, item_name).get()
return file["Body"].read()
except ClientError as be:
w={"CLIENT ERROR":be}
print(json.dumps(w))
except Exception as e:
y={"Unable to retrieve file contents":e}
print(json.dumps(y))
def test():
x = get_item('cloud-college-bucket0','abc.txt')
print(x.decode('utf-8'))
if x is not None:
string_in_uppercase = x.upper();
n={"String in Uppercase =":string_in_uppercase.decode('utf-8')}
b=json.dumps(n)
print(b)
def main(dict):
return test()
if __name__ == '__main__':
main()
Error it throws:
Results:
{
"error": "**The action did not produce a valid JSON response**: null\n"
}
Logs:
[
"2019-10-08T13:01:56.339677Z stderr: /usr/local/lib/python3.7/site-packages/ibm_botocore/vendored/requests/api.py:67: DeprecationWarning: You are using the post() function from 'ibm_botocore.vendored.requests'. This is not a public API in ibm_botocore and will be removed in the future. Additionally, this version of requests is out of date. We recommend you install the requests package, 'import requests' directly, and use the requests.post() function instead.",
"2019-10-08T13:01:56.339748Z stderr: DeprecationWarning",
"2019-10-08T13:01:56.339755Z stderr: /usr/local/lib/python3.7/site-packages/ibm_botocore/vendored/requests/models.py:169: DeprecationWarning:
Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working",
"2019-10-08T13:01:56.339759Z stderr: if isinstance(hook, collections.Callable):",
"2019-10-08T13:01:56.339678Z stdout: {\"Retrieving item from bucket\": \"cloud-college-bucket0\", \"key\": \"abc.txt\"}",
"2019-10-08T13:01:56.339772Z stdout: hello friends",
"2019-10-08T13:01:56.339776Z stdout: {\"String in Uppercase =\": \"HELLO FRIENDS\"}",
"2019-10-08T13:01:56.340Z stderr: The action did not initialize or run as expected. Log data might be missing."
]
It says import request which I did but still the problem persists.
I also say use request.post function but how and where to use is what I am unable to understand. And how to solve this JSON issue?
And the desired output is shown in the logs.
From the first outset, I could see that the test function only prints the JSON but never returns it. But you if you see the sample Python action, it always should return a JSON
import sys
def main(dict):
return { 'message': 'Hello world' }
Also, you can check the supported packages list with Python runtime here before using them in your action.
If you have a package that is not in the list, you can always package Python code with a virtual environment in .zip files or Packaging code in Docker images

Laravel - Python script execution

I have to execute a Python script which pulls a large amount of data to the database. It is working fine while I am running a project using the command php artisan serve, but it is throwing an error after calling the public folder URL given below
localhost/project/public
Error:
The command "python /opt/lampp/htdocs/laravel/projectname/beta/projectname/public/python/pubmed_abstract/pubmed_engine.py '{"term":"cancer"}'" failed.
Exit Code: 1(General error) Working directory: /opt/lampp/htdocs/laravel/infocytosis/beta/infocytosis/public Output:
================ Error Output: ================
Traceback (most recent call last): File "/opt/lampp/htdocs/laravel/projectname/beta/projectname/public/python/pubmed_abstract/pubmed_engine.py", line 5, in from Bio import Entrez ImportError: No module named Bio
Code I used:
$python_path=public_path().'/python/doom_abstract/doom_engine.py';
$variables='{"term":"'.addslashes($request->term).'"}';
$process = new Process("python $python_path '$variables'");
$process->run();
return redirect()->back()->withMessage('Filter saved successfully');
You can try following one
$python_path=public_path('python/doom_abstract/doom_engine.py');
Instead of:
$process = new Process("python $python_path '$variables'");
$process->run();
how about
shell_exec("python $python_path '$variables'")

Perfectly running python script gives error when run from web.py

I have the following python script which runs perfectly if run separately:
import arcpy
val = arcpy.GetCellValue_management("D:\dem-merged\lidar_wsg84", "-95.090174910630012 29.973962146120652", "")
print str(val)
I want to expose this as a web service and so I installed web.py and wrote the following code for code.py. but it gives errors(when invoked. compiles fine).
import web
import arcpy
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
val = arcpy.GetCellValue_management("D:\dem-merged\lidar_wsg84", "-95.090174910630012 29.973962146120652", "")
return str(val)
if __name__ == "__main__":
app.run()
When i invoke this using http://localhost:8080, i get the following error:
<class 'arcgisscripting.ExecuteError'> at /
ERROR 000582: Error occurred during execution.
Python C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py in GetCellValue, line 8460
Web GET http://localhost:8080/
Traceback (innermost first)
C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py in GetCellValue
be returned."""
try:
retval = convertArcObjectToPythonObject(gp.GetCellValue_management(*gp_fixargs((in_raster, location_point, band_index), True)))
return retval
except Exception, e:
raise e ...
#gptooldoc('GetRasterProperties_management', None)
def GetRasterProperties(in_raster=None, property_type=None):
"""GetRasterProperties_management(in_raster, {property_type})
Returns the properties of a raster dataset.
I have tried Debugging it in many ways, the code .py runs fine if i just return a "hello". The library i am using is a ArcGIS Geoprocessing toolbox library.
Any ideas as to what might be wrong?
That looks like an error from the third party module. Try finding out what that error code (000582) actually means to the application.

Categories