I’ve tried looking for a similar question but couldn’t find one. Sorry if this question has already been asked.
To my question, I’ve created a flask application that creates a database instance using SQLalchemy. My question is how do I access this .db file from my command prompt? I’d like for example get a list of all the users. In my app I’d write User.query.all() but it gets messy quickly. I’d rather just do it from the command line on my Mac if possible.
Thanks in advance!!
What I’ve tried from the terminal:
sqlite3 database.db
User.query.all() (nothing happens)
And then:
python3 main.py
User.query.all() (nothing happens)
sqlite3 is correct to open the database file. you're going to need to use SQL statements from terminal:
sqlite3 database.db
show tables;
select * from Users; # depends on your table names, obviously
insert into Users... # and so on, please refer to sqlite3 manual if you need to or ask a direct question about what you're trying to manually do to the database
The python3 command is just going to run the interpreter across main.py.. I think flask is started with flask run but you're probably looking for flask shell .. it's been a minute for me on flask, sorry.
https://www.sqlite.org/cli.html
Related
i'm new to python and literely trying to import the module toolbox in the createRecipe.py file. Please see picture as it describes my problem
importProblem
I've looed in over 30 resources on the Internet and it seems there is no working solution for it. Though, i would like to raise the problem again, perhaps there is something new in python which makes this possible
I'm currently building an user friendly program in Python. Currently, the user is able to modify the input values provided in an init script that we can call init.py. At this moment the user can open in Spyder, the main.py and run/execute the whole process or just typing the classical command:
python3 main.py
The main.py file import all the variables needed from the init.py and run normally. What I would like to do now is to add a feature which allows the user to change the name of the init.py file. For example to be able to build initcustom.py.
And use the following command :
python3 main.py initcustom.py
How can I be able to import variables in main.py from a script which can change name (that should be provided by the user in the command line)?
And in the case where nothing is specified we keep the classical init.py
What such feature will induce as changes in the case where someone just want to do F5 using Spyder without precising input names?
Thank you in advance for your help
You can probably do something with exec
import sys
exec(‘import’+’sys.argv[2]’)
I've been trying to see if I can use python-decouple to place my bot credentials on a separate .env file.
Auth method is basically right off the praw doc:
reddit = praw.Reddit(
client_id=config('CLIENT_ID'),
client_secret=config('CLIENT_SECRET'),
password=config('PASSWORD'),
user_agent=config('USER_AGENT'),
username=config('USERNAME')
)
However, whenever I try it, it seems to return an 403 auth error. I work my way back, replacing the decouple configs with strings of the actual details, but it doesn't seem to follow through, and the errors that occur seem random depending on what and when things I take out.
Is this a problem with how decouple functions?
Thanks.
I've been trying to see if I can use python-decouple to place my bot credentials on a separate .env file.
Why not use a praw.ini file? This is documented here in PRAW documentation. It's a format for storing Reddit credentials in a separate file from your code. For example, a praw.ini file may look like:
[bot1]
client_id=Y4PJOclpDQy3xZ
client_secret=UkGLTe6oqsMk5nHCJTHLrwgvHpr
password=pni9ubeht4wd50gk
username=fakebot1
[bot2]
client_id=6abrJJdcIqbclb
client_secret=Kcn6Bj8CClyu4FjVO77MYlTynfj
password=mi1ky2qzpiq8s59j
username=fakebot2
You then use specific credentials in your code like so:
import praw
reddit = praw.Reddit('bot2', user_agent='myBot v0.1')
print('Logged in as', reddit.user.me())
I think this is the best solution for working with PRAW credentials.
However, if you really want to do it with python-decouple, here's a working example:
Contents of file .env:
username=k8IA
password=REDACTED
client_id=REDACTED
client_secret=REDACTED
Contents of file connect.py:
import praw
from decouple import config
reddit = praw.Reddit(username=config('username'),
password=config('password'),
client_id=config('client_id'),
client_secret=config('client_secret'),
user_agent='myBot v0.1')
print('Logged in as', reddit.user.me())
Output when running python3 connect.py:
Logged in as k8IA
I wrote a simple set of python3 files for emulating a small set of mongodb features on a 32 bit platform. I fired up PyCharm and put together a directory that looked like:
minu/
client.py
database.py
collection.py
test_client.py
test_database.py
test_client.py
My imports are simple. For example, client.py has the following at the top:
from collection import Collection
Basically, client has a Client class, collection has a Collection class, and database has a Database class. Not too tough.
As long as I cd into the minu directory, I can fire up a python3 interpreter and do things like:
>>> from client import Client
>>> c = Client(pathstring='something')
And everything just works. I can run the test_files as well, which use the same sorts of imports.
I'd like to modularize this, so I can use it another project by just dropping the minu directory alongside my application's .py files and just have everything work. When I do this though, and am running python3 from another directory, the local imports don't work. I placed an empty init.py in the minu directory. That made it so I could import minu. But the others broke. I tried using things like from .collection import Collection (added the dot), but then I can't run things in the original directory anymore, like I could before. What is the simple/right way to do this?
I have looked around a bit with Dr. Google, but none of the examples really clarify it well, feel free to point out the one I missed
In this file ...minu/__init__.py import the submodules you wish to expose externally.
If the __init__.py file contains the following lines, and the client.py file has a variable foo.
import client
import collection
import database
Then from above the minu directory, the following will work:
from minu.client import foo
db: mysql
lang: python
framework:
django
Operating System: Linux (ubuntu)
Hello,
Is there a way to execute a python against a content of a script that is stored in a database? For example, a content of a file is stored in a db column text. Would the only solution be to create a temporary file, dump the content from the db into the file and then run python os command against it? I'm assuming the content of the executed script will need to be stored such that it escapes quotes etc.
I'm open to suggestions on what database to use to accomplish my goal. MySQL will require additional wrappers before storage of the file content and possibly apply others to reply qoutes/datetime/etc.
Please advise if additional information necessary, but in essence i'm looking to store python script content in a db, retrieve it and run it against the python interpreter.
Thank you in advance for any advise.
You can use the compile built in function.
s = """def f(x):
return x + x
print(f(22))
"""
code = compile(s, "string", "exec")
exec(code)
# OUT: 44
Although I'm wondering if you couldn't just store a data structure and use that with some pre-defined code. Executing arbitrary code in this way could be dangerous, and a security risk.
This seems very similar to SO post here:
Dynamically loading Python application code from database under Google App Engine
Here is information on exec
http://docs.python.org/release/2.5.2/ref/exec.html
Python Wiki page for Python+MySQL
http://wiki.python.org/moin/MySQL