CherryPy cannot import name safemime - python

Trying to import safemine from cherrypy
from cherrypy import safemime
ImportError: cannot import name safemime
Cherrypy is installed with PIP ie.
sudo pip install -U cherrypy

According to cherrypy-users mailing list, safemime module was removed:
That module was removed in http://www.cherrypy.org/changeset/2319
because its functionality was folded into the new _cpreqbody.py
module.

Related

Error with Python SDK for IBM Watson when importing into Pycharm

The error I receive is:
DeprecationWarning: watson-developer-cloud moved to ibm-watson. To get
updates, use the new package.
service = watson_developer_cloud.AssistantV1(
I have tried updating watson-developer-cloud using pip install however I still have the same error.
The code used is below. All done in Python. Just left out the API key from the original code.
Any help is appreciated.
service = watson_developer_cloud.AssistantV1(
iam_apikey= '',
version= '2021-01-20'
import os
from pathlib import Path
import slack
import ibm_watson
import ibm_cloud_sdk_core
import watson_developer_cloud
from ibm_watson import AssistantV1
from dotenv import load_dotenv
)
See here for the instructions on that Python package for IBM Watson services. It is like stated in the warning:
watson-developer-cloud is now named ibm-watson. What you have to do is
pip install ibm-watson
or
pip install --upgrade ibm-watson
Because the packagae is named ibm-watson, you would need to use that name for import...
import ibm-watson
or
from ibm_watson import AssistantV1
See the linked repository for examples.

pip-18.0 has 'logging module not found' error

Having just upgraded to pip-18.0
all uses of pip produce this error:
...\site-packages\pip\_internal\__init__.py", line 5, in <module>
import logging
ModuleNotFoundError: No module named 'logging'
logging.py exists in \site-packages\pip\_internal\utils\
(and also contains an 'import logging' statement!)
I suppose I could move it up a directory level or update _init to 'import utils.logging'
In fact lots of my programs' packages eg numpy are now failing with no logging module. I have now identified the problem:
import logging
Traceback (most recent call last):
File "", line 1, in
import logging
ModuleNotFoundError: No module named 'logging'
import lib2to3.logging
lib2to3 is a subdir of Lib, containing the logging module,
but is no longer found in the module search.
In my case, running OpenWrt/LEDE 17.01.4 on the client, I had installed most of Python3 via:
opkg install python3-light
when I received the ImportError: No module named logging. I needed to separately install the logging package:
opkg install python3-logging
The above is enough to run the Ansible ping module. To run the setup module you also need:
opkg install python3-multiprocessing python3-distutils
I fixed it by moving logging up from Lib/lib2to3/ to Lib/
where import canm now find it.
I would add to #bitinerant 's answer that also the following package needs to be installed:
opkg install python-openssl python-codecs
for the ping and setup modules to work on python2 based systems

No module named "requests" when trying to use google OAuth2 with Docker

I am trying to use google OAuth for my web app. To do so I installed the packages google-api-python-client and google-auth in my venv and during my Docker build(from a requirements.txt). Despite this when I run my app it can't find the requests module, complaining that:
flask.cli.NoAppException: While importing "debateit", an ImportError was raised:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/google/auth/transport/requests.py", line 23, in <module>
import requests
ImportError: No module named 'requests'
The import is as follows:
from google.auth.transport import requests
and is used like:
idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
Other imports such as the id_token.verify_oauth2_token work fine.
I checked my docker build and it says I have included google-auth correctly:
Installing collected packages: ... google-auth, httplib2, google-auth-httplib2, google-api-python-client
Successfully installed ... google-api-python-client-1.7.3 google-auth-1.5.0 google-auth-httplib2-0.0.3 httplib2-0.11.3 ...
I can clearly see the google.auth.transport.requests module when I look in the venv, it just doesn't work in the app itself.
What am I missing? What could cause this module to not be found?
So I found out what was wrong - within the google.auth.transport.requests module they try to import the library "requests". I did not have this library installed. I have done so and it works now.
The guide I was following: https://developers.google.com/identity/sign-in/web/backend-auth did not mention that you need to install this library. I misunderstood what the import for requests in the requests module was supposed to do.
As reported in the documentation, it should be more likely like:
import google.auth.transport.requests
import requests
request = google.auth.transport.requests.Request()
credentials.refresh(request)
But for your purpose I'll suggest:
from google.auth.transport.requests import Request
then change the following from:
idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
to:
idinfo = id_token.verify_oauth2_token(token, Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
It was an error because inside the path google.auth.transport.requests there is no function or class which is named requests.
My suggestion is based on the line
idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
which show us that you use a class named Requests() which is present into google.auth.transport.requests as you can see in the documentation.
I know this was answered but you can quickly install those libraries globally by the running the command :
pip install google-auth
Here is how to install pip or installing the libraries in local virtual environment if someone doesn't want to install them globally:
https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/
pip install requests
It seems like google.auth.transport.requests uses requests.

Python module from GitHub installed using setup.py can't see own submodules

I downloaded python-somelib-master.zip from GitHub hoping to use the API it provides. I ran
python setup.py install
And it apparently completed successfully:
Writing D:\SOFT\Python3\Lib\site-packages\python-somelib-1.0-py3.5.egg-info
which then introduced D:\SOFT\Python3\Lib\site-packages\somelib.
However, when I try to import something from there in my script:
from somelib import SubModule
I get
File "D:\SOFT\Python3\lib\site-packages\somelib\__init__.py", line 1, in <module>
from base import SubModule
ImportError: No module named 'base'
I confirmed that base.py is present in D:\SOFT\Python3\Lib\site-packages\somelib.
Did I not properly install the module?
You must install modules dependencies in your machine too. If you are using Ubuntu, you can easily do it with "apt-get". Hope it helps! xD

Python 3.x - How to get the directory where user installed package

Suppose that I am building a Python package with the following folder tree:
main
|-- setup.py
|-- the_package
|--globar_vars.py
|--the_main_script.py
When the user performs anyway to install it, like:
sudo python setup.py install
python setup.py install --user
python setup.py install --prefix=/usr/local
Or well, using PIP:
pip install 'SomeProject'
I want that the folder where the package was installed be saved on the global_vars.py, in any variable, Eg:
globarl_vars.py
#!/usr/bin/env python3
user_installed_pkg = '/some/path/where/the/package/was/installed'
There is someway to get it? Thanks in advance.
Assuming your SomeProject package is a well-formed Python package (which can be done using setuptools), a user can derive the location simply use the pkg_resources module provided by setuptools package to get this information. For example:
>>> from pkg_resources import working_set
>>> from pkg_resources import Requirement
>>> working_set.find(Requirement.parse('requests'))
requests 2.2.1 (/usr/lib/python3/dist-packages)
>>> working_set.find(Requirement.parse('requests')).location
'/usr/lib/python3/dist-packages'
However, the path returned could be something inside an egg which means it will not be a path directly usable through standard filesystem tools. You generally want to use the Resource manager API to access resources in those cases.
>>> import pkg_resources
>>> api_src = pkg_resources.resource_string('requests', 'api.py')
>>> api_src[:25]
b'# -*- coding: utf-8 -*-\n\n'
import wtv_module
print(wtv_module.__file__)
import os
print(os.path.dirname(wtv_module.__file__))

Categories

Resources