Import errors in telegram bot (python-telegram-bot) - python

I have a telegram bot written using python-telegram-bot that was working fine but after a few weeks, when I start the script, it seems like it cannot find some libraries & modules.
As an example, "Update" is widely used in my code and it was working just fine. But now it cannot be found by python.
Here are some of my imports.
from telegram import Update, ForceReply, InlineQueryResultArticle, InputTextMessageContent, ReplyKeyboardRemove, InlineKeyboardButton, InlineKeyboardMarkup, Update, User, ReplyKeyboardMarkup, Contact, Message, KeyboardButton
And here is the error.
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
NameError: name 'Update' is not defined
Does anyone know any solution?
I tried moving to a new environment, reinstalling python-telegram-bot with --pre or --upgrade switches, clearing the python cache and reloading the vs code ssh window.

I don't think there is a good way to transfer environments to production (Correct me if I am wrong), however, you can build a new environment using the exact same packages that are installed in your environment using:
pip freeze > requirements.txt
This saves every package you used in the development env in a file.
Then you can use this file in a docker file to build the exact same dependencies your bot was developed with, using:
pip install -r requirements.txt

Related

Install Python modules in Azure Functions

I am learning how to use Azure functions and using my web scraping script in it.
It uses BeautifulSoup (bs4) and pymysql modules.
It works fine when I tried it locally in the virtual environment as per this MS guide:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function-azure-cli?pivots=programming-language-python&tabs=cmd%2Cbrowser#run-the-function-locally
But when I create the function App and publish the script to it, Azure Functions logs give me this error:
Failure Exception: ModuleNotFoundError: No module named 'pymysql'.
It must happen when attempting to import it.
I really don't know how to proceed, where should I specify what modules it needs to install?
You need to check if you have generated the requirements.txt which includes all of the information of the modules. When you deploy the function to azure, it will install the modules by the requirements.txt automatically.
You can generate the information of modules in requirements.txt file by the command below in local:
pip freeze > requirements.txt
And then deploy the function to azure by running the publish command:
func azure functionapp publish hurypyfunapp --build remote
For more information about deploy python function from local to auzre, please refer to this tutorial.
By the way, if you use consumption plan for your python function, the "Kudu" is not available for us. If you want to use "Kudu", you need to create app service plan for it but not consumption plan.
Hope it helps~
You need to upload the installed modules when deploying to azure. You can upload them using Kudu:
https://github.com/projectkudu/kudu/wiki/Kudu-console
as an alternative, you can also use Kudu and run pip install using the console:
Install python packages from the python code itself with the following snippet: (Tried and verified on Azure functions)
def install(package):
# This function will install a package if it is not present
from importlib import import_module
try:
import_module(package)
except:
from sys import executable as se
from subprocess import check_call
check_call([se,'-m','pip','-q','install',package])
for package in ['beautifulsoup4','pymysql']:
install(package)
Desired libraries mentioned the list gets installed when the azure function is triggered for the first time. for the subsequent triggers, you can comment/ remove the installation code.

I'm writing a telegram bot with python

I want to write a telegram bot via Python, but it doesn't work.
import telebot
bot = telebot.TeleBot("my_token")
#bot.message_handler(content_types=['text'])
def sending(message):
bot.send_message(message.chat.id, message.text)
# RUN
bot.polling(non_stop=True)
Returns to me the following problem.
AttributeError: 'TeleBot' object has no attribute 'message_handler'
This is a common issue, unfortunately. I guess you installed the lib as "pip install telebot", which leads to another package. You need to uninstall telebot, install pytelegrambotapi, but leave "import telebot" in code.
Try this one. At first write:
pip3 uninstall telebot
After you do it write:
pip3 uninstall PyTelegramBotAPI
And the last step:
pip3 install PyTelegramBotAPI==2.2.3
It worked for me, I'm working on debian 9. So if you are working on Debian or on linux, it should work for you.
As the source code shows (assuming you import the module obtained from pip, that is this), there is no definition for message_handler. In which case you need to use #bot.route which takes a string as argument as shown in the example within the repository readme (second link or here).
Example:
#bot.route('/command ?(.*)')
def sending(message, cmd):
bot.send_message(something, something_else)

How can I make this script run

I found this script (tutorial) on GitHub (https://github.com/amyoshino/Dash_Tutorial_Series/blob/master/ex4.py) and I am trying to run in my local machine.
Unfortunately I am having and Error
I would really appreciate if anyone can help me to run this script.
Perhaps this is something easy but I am new in coding.
Thank you!
You probably just need to pip install the dash-core-components library!
Take a look at the Dash Installation documentation. It currently recommends running these commands:
pip install dash==0.38.0 # The core dash backend
pip install dash-html-components==0.13.5 # HTML components
pip install dash-core-components==0.43.1 # Supercharged components
pip install dash-table==3.5.0 # Interactive DataTable component (new!)
pip install dash-daq==0.1.0 # DAQ components (newly open-sourced!)
For more info on using pip to install Python packages, see: Installing Packages.
If you have run those commands, and Flask still throws that error, you may be having a path/environment issue, and should provide more info in your question about your Python setup.
Also, just to give you a sense of how to interpret this error message:
It's often easiest to start at the bottom and work your way up.
Here, the bottommost message is a FileNotFound error.
The program is looking for the file in your Python37/lib/site-packages folder. That tells you it's looking for a Python package. That is the directory to which Python packages get installed when you use a tool like pip.

can't import google.appengine.api

This should be pretty basic as I've installed lots of python packages, but I can't for the life of my get google apis client library for python to install.
I'm pressure sure I've gone through the instructions on this website properly:
https://developers.google.com/api-client-library/python/start/installation
To summarize I've done the following:
$ easy_install --upgrade google-api-python-client
this seems to work fine, doesn't report any errors or warnings
downloaded and unzipped google-api-python-client-gae-1.2.zip into the directory where my project is
but if I open an iPython session in the folder where I unpacked the full dependencies I can't do the basic imports such as:
import google.appengine.api it just says "No Module named google.appengine.api"
I checked in my site-packages folder and google_api_python_client-1.2-py2.7.egg is there. But it doesn't show up in sys.path
when I do sys.path.append('C:\Anaconda\Lib\site-packages\google_api_python_cli
ent-1.2-py2.7.egg') it adds the correct path, but the import still doesn't work.
EDIT: This fixed my problem
Adding the Google SDK to my Python path did the trick. I don't know why the installer didn't do this when I ran it. but hey, this worked
So if I run:
sys.path.append('C:\\Program Files (x86)\\Google\\google_appengine')
from google.appengine import api
works!
Without knowing all the steps you took to install app engine and the client APIs, my only recommendation is to install (or reinstall) the Google App Engine Python SDK from here: https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python. Looks like you're on windows, so you would grab the MSI.
Just make sure you launch the AppEngineLauncher application after installation as it will give you the option to create symlinks so you can run commands from terminal.

Installing PRAW

I would like to install PRAW so I can make reddit bots and stuff, but all the install guides are confusing to me so could someone explain how to as noob friendly as possible. I've had some experience with vanilla python. Thanks!
praw is best installed, according to the documentation, via pip. To install pip, you need setuptools. Here is a simple guide on installing pip via setuptools.
Basically, download ez_setup.py and get-pip.py, two Python scripts that automatically get and install setuptools and pip. You'll want to run the following commands in the terminal in the same directory as the location of the files, in order:
python ez_setup.py
python get-pip.py
Finally, you'll want to use pip to get praw. pip is an executable file that is usually located in your python build directory. For example, in Windows, it's located in C:\Python27\scripts. You can add that directory to your system path variable, but right now you can just navigate to that directory where pip.exe is installed. Then, run the following command in the terminal:
pip install praw
I recently had trouble with this so I thought I would add what I did.
Install Pip - https://pip.pypa.io/en/stable/installing/
install praw pip install praw; This is done on your pc/mac/linux(?)
Installation guide
Register on reddit as a developer and register the app. To be able to use the api you need to have a client_id and a client_secret. Get those by registering here. More information about the types of applications can be found here.
Now you are ready to begin coding. This is a good script to verify that you are connecting to the reddit api. The client_id and client_secret are from the previous step and the user_agent is a string that is unique to your app. I used something like 'my first app by /u/myUsername'. The password and username is your reddit login
Run this code and it should output your username.
import praw
reddit = praw.Reddit(client_id='CLIENT_ID',
client_secret="CLIENT_SECRET", password='PASSWORD',
user_agent='USERAGENT', username='USERNAME')
print(reddit.user.me())

Categories