I'm a beginner at python. I'm trying to connect to Mongo DB server from my python flask application. But I couldn't able to run the application as I'm facing the below problem.
from flask import Flask, render_template
from flask_pymongo import PyMongo
from pymongo import MongoClient # Database connector
app=Flask(__name__)
app.config["MONGO_DBNAME"]="connect_to_pymon"
app.config["MONGO_URI"]="mongodb://mongo_test:mongo_test#123#ds129821.mlab.com:29821/connect_to_pymon"
mongo = PyMongo(app)
#app.route("/add")
def add():
user = mongo.db.users
user.insert[{"name":"kishor"}]
return "users added!"
if __name__=='__main__':
app.run(debug=True, port=8080)
This is my source code. When I execute this I'm getting the following error which I couldn't able to trace it.
Traceback (most recent call last):
File "C:/Users/kravi/PycharmProjects/GitUploadTest/mongoTest.py", line 9, in <module>
mongo = PyMongo(app)
File "C:\Kishor\Training\My_Workspace\Python_Basics\TaskCRUD\venv\lib\site-packages\flask_pymongo\__init__.py", line 116, in __init__
self.init_app(app, uri, *args, **kwargs)
File "C:\Kishor\Training\My_Workspace\Python_Basics\TaskCRUD\venv\lib\site-packages\flask_pymongo\__init__.py", line 149, in init_app
parsed_uri = uri_parser.parse_uri(uri)
File "C:\Kishor\Training\My_Workspace\Python_Basics\TaskCRUD\venv\lib\site-packages\pymongo\uri_parser.py", line 379, in parse_uri
user, passwd = parse_userinfo(userinfo)
File "C:\Kishor\Training\My_Workspace\Python_Basics\TaskCRUD\venv\lib\site-packages\pymongo\uri_parser.py", line 97, in parse_userinfo
"RFC 3986, use %s()." % quote_fn)
pymongo.errors.InvalidURI: Username and password must be escaped according to RFC 3986, use urllib.parse.quote_plus().
Process finished with exit code 1
Note: I haven't installed mongoDb in my PC. I'm using mongo db as a service
you can connect to your db using PyMongo as follows:
from pymongo import MongoClient
client = MongoClient("mongodb://mongo_test:mongo_test#123#ds129821.mlab.com:29821/connect_to_pymon")
db = client["dbname"] # connect_to_pymon in your case
I understood that using special characters like "#" in passwords doesn't make sense as we have to configure mongo URI which has a specific format with symbol # in it. I changed the database password and tried, after which it worked.!
Related
>>> from influxdb import InfluxDBClient
>>> from datetime import datetime
>>> client = InfluxDBClient('localhost', 8086, 'root', '<Password>')
>>> client.get_list_database()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/<USername>/.local/lib/python3.6/site-packages/influxdb/client.py", line 704, in get_list_database
return list(self.query("SHOW DATABASES").get_points())
File "/home/<USername>/.local/lib/python3.6/site-packages/influxdb/client.py", line 527, in query
expected_response_code=expected_response_code
File "/home/<USername>/.local/lib/python3.6/site-packages/influxdb/client.py", line 378, in request
raise InfluxDBClientError(err_msg, response.status_code)
influxdb.exceptions.InfluxDBClientError: 401: {"code":"unauthorized","message":"Unauthorized"}
Can someone please guide me what I'm doing wrong here.
It seems that, this issue very clear, according to what you wrote as your instructions to connect to InfluxDB and in this line:
client = InfluxDBClient('localhost', 8086, 'root', '<Password>')
It seems that you used a sample command to connect to your database(I know it from something like '<Password>' as your password to connect to database), and because of this you get 401 error which means your credentials is not true, write these credentials yourself and which are True for your InfluxDB.
This is my first time trying to make a telegram bot.
Code:
import os
import telebot
API_TOKEN = os.getenv('API_KEY')
bot = telebot.TeleBot(API_TOKEN)
#bot.message_handler(commands=['hello'])
def send_welcome(message):
bot.reply_to(message, "HI!")
bot.polling()
Error:
Traceback (most recent call last):
File "/Users/anshtyagi/Documents/telegram bot/main.py", line 23, in <module>
bot.polling()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 621, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 695, in __threaded_polling
raise e
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 650, in __threaded_polling
polling_thread.raise_exceptions()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/util.py", line 111, in raise_exceptions
raise self.exception_info
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/util.py", line 93, in run
task(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 360, in __retrieve_updates
updates = self.get_updates(offset=(self.last_update_id + 1),
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/__init__.py", line 338, in get_updates
json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates, long_polling_timeout)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/apihelper.py", line 324, in get_updates
return _make_request(token, method_url, params=payload)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/telebot/apihelper.py", line 80, in _make_request
raise Exception('Bot token is not defined')
Exception: Bot token is not defined
I am getting this error while running my telegram bot this is my first time. I have seen some tutorials how to make a bot but mine is not working. I have posted my token in .env file and imported it using os.getenv. I am just simply trying to make a simple bot just for my knowledge as I have tried making discord bots and it was great experience and I learned many new languages so I thought why not try this also.
I'm also new to python and telegram bots, but my first bot is running ok and I see the problem in your code. Your exception tells us Exception: Bot token is not defined. So your script doesn't get a token from the system var.
You say that you have your token in .env file. To get os variable from that file first of all we must import a module: from dotenv import load_dotenv.
Next we must import variables from that file using the function load_dotenv().
After that we can read our variables with os.getenv. So that line of your code seems to be correct. If there is no variable in the file os.getenv should get it from the os.
So your code may look like:
import os
import telebot
from dotenv import load_dotenv
load_dotenv()
API_TOKEN = os.getenv('API_KEY')
bot = telebot.TeleBot(API_TOKEN)
#bot.message_handler(commands=['hello'])
def send_welcome(message):
bot.reply_to(message, "HI!")
bot.polling()
try this:
import telebot
bot = telebot.TeleBot("API_KEY")
instead of:
import os
import telebot
API_TOKEN = os.getenv('API_KEY')
bot = telebot.TeleBot(API_TOKEN)
I'm converting a Google App Engine Python 2 project to Python 3
My understanding from reading the documentation I understand the preferred path is to run the main python program directly, skipping dev_appserver.py as was done in the past.
https://cloud.google.com/appengine/docs/standard/python3/tools/local-devserver-command
python3 main.py
--- main.py --
# coding: utf-8
import flask
from flask import request, redirect, url_for
from flask_basicauth import BasicAuth
#from urllib# import urlparse, urlunparse
import config
--- config.py
import model
...
33: CONFIG_DB = model.Config.get_master_db()
--- model/config.py
from __future__ import absolute_import
from google.cloud import ndb
#from oauth2client.contrib.appengine import CredentialsNDBProperty
from api import fields
import config
import model
import util
...
class Config(model.Base, model.ConfigAuth):
...
#classmethod
def get_master_db(cls):
57: return cls.get_or_insert('master')
When running the following trace is prod
Traceback (most recent call last):
File "main.py", line 9, in <module>
import config
File "/home/ffej/cloudpayback/main/config.py", line 33, in <module>
CONFIG_DB = model.Config.get_master_db()
File "/home/ffej/cloudpayback/main/model/config.py", line 57, in get_master_db
return cls.get_or_insert('master')
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/_options.py", line 89, in wrapper
return wrapped(*pass_args, **kwargs)
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/utils.py", line 146, in positional_wrapper
return wrapped(*args, **kwds)
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/model.py", line 5698, in _get_or_insert
return cls._get_or_insert_async(
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/_options.py", line 89, in wrapper
return wrapped(*pass_args, **kwargs)
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/utils.py", line 146, in positional_wrapper
return wrapped(*args, **kwds)
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/model.py", line 5811, in _get_or_insert_async
key = key_module.Key(cls._get_kind(), name, parent=parent, **key_args)
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/key.py", line 290, in __new__
context = context_module.get_context()
File "/home/ffej/cloudpayback/env/lib/python3.8/site-packages/google/cloud/ndb/context.py", line 96, in get_context
raise exceptions.ContextError()
google.cloud.ndb.exceptions.ContextError: No current context. NDB calls must be made in context established by google.cloud.ndb.Client.context.
Is there additional logic that should be included after migration to start/initialize the datastore?
Thanks,
Jeff
The API for NDB library in Python3 has changed significantly. For developing on localhost you have to:
run DataStore Emulator, since you're not running dev_appserver anymore:
$ gcloud beta emulators datastore start
if you use the new NDB Library, then each NDB operation needs to be wrapped in a context manager:
with ndb_client.context(): # <- you need this line
cls.get_or_insert('master')
edit: instead of wrapping each NDB call with a context manager, you can use a middleware which will wrap the whole request cycle into the NDB context:
class NDBMiddleware:
def __init__(self, app):
self.app = app
self.client = ndb_client
def __call__(self, environ, start_response):
with self.client.context():
return self.app(environ, start_response)
app = Flask(__name__)
app.wsgi_app = NDBMiddleware(app.wsgi_app)
I'm trying to write my own little python flask app to monitor the hard drives of my server.
But since now, I'm getting trouble using the sqljobstore of apscheduler.
While the server is running, everything is fine. But after a restart, I can't access the web interface and getting the folowing output:
Unable to restore job "refresh_disks" -- removing it
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/apscheduler/util.py", line 289, in ref_to_obj
obj = getattr(obj, name)
AttributeError: module 'dirkules.tasks' has no attribute 'refresh_disks'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/apscheduler/jobstores/sqlalchemy.py", line 141, in _get_jobs
jobs.append(self._reconstitute_job(row.job_state))
File "/usr/local/lib/python3.6/dist-packages/apscheduler/jobstores/sqlalchemy.py", line 128, in _reconstitute_job
job.__setstate__(job_state)
File "/usr/local/lib/python3.6/dist-packages/apscheduler/job.py", line 272, in __setstate__
self.func = ref_to_obj(self.func_ref)
File "/usr/local/lib/python3.6/dist-packages/apscheduler/util.py", line 292, in ref_to_obj
raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference dirkules.tasks:refresh_disks: error looking up object
[2019-04-26 15:46:39 +0200] [13296] [INFO] Shutting down: Master
Here is my config.py:
import os
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
#from apscheduler.jobstores.memory import MemoryJobStore
baseDir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(baseDir, 'dirkules.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
# The SCHEDULER_JOB_DEFAULTS configuration is per job, that means each job can execute at most 3 threads at the same time.
# The SCHEDULER_EXECUTORS is a global configuration, in this case, only 1 thread will be used for all the jobs.
# I believe the best way for you is to use max_workers: 1 when running locally
SCHEDULER_JOBSTORES = {'default': SQLAlchemyJobStore(url='sqlite:///' + os.path.join(baseDir, 'dirkules.db'))}
#SCHEDULER_JOBSTORES = {'default': MemoryJobStore()}
SCHEDULER_EXECUTORS = {'default': {'type': 'threadpool', 'max_workers': 3}}
SCHEDULER_JOB_DEFAULTS = {'coalesce': False, 'max_instances': 1}
SCHEDULER_API_ENABLED = True
init.py:
import dirkules.config as config
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_apscheduler import APScheduler
app = Flask(__name__)
app.config.from_object(config)
db = SQLAlchemy(app)
import dirkules.models
db.create_all()
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
##app.before_first_request
from dirkules import tasks
# from dirkules.models import Time
# from sqlalchemy.orm.exc import NoResultFound
#
# try:
# Time.query.one()
# except NoResultFound:
# db.session.add(Time("Drives"))
# db.session.commit()
import dirkules.views
and tasks.py:
from dirkules import scheduler
import datetime
import dirkules.driveManagement.driveController as drico
#scheduler.task('interval', id='refresh_disks', seconds=10)
def refresh_disks():
#drives = drico.getAllDrives()
print("Drives refreshed")
Hopefully, you can help me!
Starting the scheduler as a side-effect of importing the module is considered bad practice and is also the likely reason why the attribute lookup fails. I would have to see a simplified, more complete example that reproduces the problem to be sure, however.
I get this error when i start my app. Obviously it has something with SQLAlchemy. I 've been working this example with help of Corey Schaffer Flask tutorial.
> File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Matea\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Matea\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Matea\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Matea\myblog\myblog\main\routes.py", line 11, in home
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\orm\scoping.py", line 74, in __call__
return self.registry()
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\util\_collections.py", line 1001, in __call__
return self.registry.setdefault(key, self.createfunc())
File "C:\Users\Matea\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2950, in __call__
return self.class_(**local_kw)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 877, in engine
return self.get_engine()
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 896, in get_engine
return connector.get_engine()
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 556, in get_engine
self._sa.apply_driver_hacks(self._app, info, options)
File "C:\Users\Matea\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 830, in apply_driver_hacks
if info.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'
here is some code:
This is from my init.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from myblog.config import Config
db = SQLAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
from myblog.users.routes import users
from myblog.posts.routes import posts
from myblog.main.routes import main
from myblog.errors.handlers import errors
app.register_blueprint(users)
app.register_blueprint(posts)
app.register_blueprint(main)
app.register_blueprint(errors)
return app
this piece code is from config.py
import os
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URL')
MAIL_SERVER = 'smtp.googlemail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = os.environ.get('EMAIL_USER')
MAIL_PASSWORD = os.environ.get('EMAIL_PASS')
I hope that you will help me and if something is missing,please tell me, Thank you!
if you have correctly set the environment variable in the .bash_proflie
export EMAIL_USER='youremail#gmail.com'
export EMAIL_PASS='Yourpassword'
export SQLALCHEMY_DATABASE_URI='sqlite:///site.db'
export SECRET_KEY='secretkeyxxxxxxxxxxxxxxxx'
then try restarting your computer.
if it still fails
*Try to create the database in python *
from your yourflaskapp import db
db.create_all()
If your app is in blueprints
use from yourapp import create_app
app = create_app()
app.app_context().push()
Always refresh your text editor and the terminal after making changes.
Also try to make sure you are running your app in the right directory.
for more info visit on blueprints use
http://flask-sqlalchemy.pocoo.org/2.3/contexts/
#Ivan M, I'm going through the same tutorial and had this problem as well. This happens because instructor moved the database URI and other params into environment variables (see Part 11 at about minute 27). It is a safety measure and good dev practice.
In order to make things work through config file, revert the changes in init.py and update config.py to have the direct link instead of square brackets:
SQLALCHEMY_DATABASE_URI = 'sqlite:///site.db'
UPD: same goes for SECRET_KEY and other params you wish to hard code:
SECRET_KEY = '5791628bb0b13ce0c676dfde280ba245'
I was following the same tutorial and got into this issue while setting up the config.py file as guided by the instructor.
Case with me
I was using PyCharm. PyCharm has the functionality to set project-specific environment variables. This means you can set environment variables w/o modifying .bash or .bash_profile, etc. files.
Mistake I Did
While setting up environment variables, we aren't supposed to have:
Spaces before and after the variable name
Quotes surrounding the value, for example:
sqlite:///site.db β
'sqlite:///site.db'β
Quotes surrounding the variable name, for example:
SQLALCHEMY_DATABASE_URI β
'SQLALCHEMY_DATABASE_URI'β
Spaces before and after the =
Spaces before and after the value
Note: If you are setting environment variables by modifying or creating .bash_profile or similar files, some of the above five points might not apply.
I did the 1st and 2nd mistake. So, silly. π
Namasteπ
Not sure if this helps. I was running into the same problems.
I put print(f'From init {Config.dict}') just below def create_app(config=Config): in init.py & can see config.py attributes from a VS Code bash terminal or from a standalone bash session - all works OK I was running python run.py from Windows terminal using powershell. Works OK from cmd with setting.bat file run first
i had the same issues with that but
installing python-dotenv it solves the issues
pip3 install python3-dotenv
or else if it doesn't then the terminal suggest to go with sudo
try with sudo as admin then it'll
it'll work after installing env
and i'll suggest one more thing....like if any guy didn't setup the environment then setup first in bashprofile for both the 'EMAIL_USER' and for 'EMAIL_PASS' and same goes for those SECRET KEY and SQLALCHEMY
to do this you have to go to your nano editor and make these environment
for LINUX
nano .bash_profile (enter then it'll take you to the nano editor)
export DB_USER="Your email"
export DB_PASS="Your password"
export SECRET_KEY="your secret key"
export SQLALCHEMY_DATABASE_URI="sqlite:///site.db"
In Bash do this
export SQLALCHEMY_DATABASE_URI='sqlite:///site.db'
and in Windows Set
SECRET_KEY
xxxx14cae56bab7xxxxxxxxxxxxx
SQLALCHEMY_DATABASE_URI
sqlite:///site.db
save everything then close terminal and close editor and restart your terminal and editor. It will work.
In my case with pyCharm only hardcoding helped or a better solution is:
import os
from dotenv import load_dotenv
load_dotenv()
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URL')
And put your vars in .env file.
.env
SQLALCHEMY_DATABASE_URI=sqlite:///site.db
Had this issue many times. The reason was Python doesn't recognize your .env file.
You can install python-dotenv by pipenv install python-dotenv.
and then
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URL')
will work.