How to setup DreamPie to mimic Django's "manage.py shell" functionality - python

I would like to use DreamPie, a python shell, as a CLI that functions the same way that manage.py shell works.
Additionally, it would be very nice to have some way to automatically run some code when the shell starts, like a set of import statements.
If you are unfamiliar with the specifics of getting this to work with DreamPie, I could also appreciate knowing what it is that manage.py shell does so I could apply it to another shell environment/interpreter.

Go to Django project directory
Execute DreamPie there
Then copy and paste manage.py on DreamPie console replacing sys.argv by ['manage.py', 'shell']

Related

How to debug a python project with shell script as the entry?

When I tried to debug some Deep Learning projects, I noticed that there are many projects use the shell script as the method of passing parameters. We can easily run these projects by bash run.sh in command line, but how to debug these kind of projects in IDEs like Pycharm?
I have tried to use some methods of python to call system commands like: os.system('bash run.sh') in debugger.py and simply debug the debugger.py. But the breakpoint set by pycharm in this method do not work, and will be directly ignored. What is the reason?
I also try to parse the passed parameters in the shell script and add these parameters in the debug configuration of pycharm like this:
debug configuration
but it seems too troublesome, especially when the incoming parameters are complex.
Is there any elegant solution to debug a python project with shell script as the entry?
Or is it not normative to write too complex parameter processing flow in shell script?

When to use python interpreter vs shell

I have a very basic question: If we want to run a script called script.py, we go to shell and type "python script.py". However, if we want to check, for example, if Django is installed or not, we first go into Python interpreter by typing "python" in the shell, and while we get the >>> then we type import Django. What is the conceptual difference? For example, in the second case, why directly running "python import Django" in the shell does not work?
python import Django tries to run a Python script named import with an argument Django.
python -c 'import Django' would attempt to execute the Python statement import Django as if you had typed it from the Python interpreter directly.
python launch interpreter. You can easly test script on it, and after create a file *.py that you can use on CRON (for ex)
When you type python and import Django it do
Open Python interpreter
Import Django library on it (to use it)
If an error raise, it seems than Django wasn't installed on computer

Execute script after `python manage.py shell` is up

There is a command that calls Django's shell:
python manage.py shell
I would like to create a BASH-alias, that will:
Start python manage.py shell
Execute print 'foo'
Something similar to -i option: python -i /usr/bin/print_foo.py but with manage.py shell
The reason for doing it is too speed up the debug process. So instead of importing all relevant models and assigning variables, I want to do it in the separate PY file. So each time I start the manage.py shell, I will just have all the tools in hand.
EDIT: using python manage.py shell < /usr/bin/print_foo.py almost does the trick. However the terminal gets closed.. if there is a way to make it stay opened.
You can use the following simple shell script:
#!/bin/bash
export PYTHONSTARTUP="$1" # Set the startup script python will run when it start.
shift # Remove the first argument, don't want to pass that.
python manage.py shell "$#" # Run manage.py with the startup script.
Just supply the python script you want to run first as the first argument to the script. All other arguments are directly passed to manage.py. The change to $PYTHONSTARTUP won't affect the environment in your shell.
I also battled with this, the < test.py closes the shell, and shell_plus doesn't add much here beside auto loading of the models, doesn't load the code you want to debug
However I can do this: I make a test.py that is initializes Django if it's not ran from a Django environment, and run it with python -i test.py
The basic idea is to not use manage.py shell
if __name__ == '__main__':
# This will run e.g. from python -i test.py, but will be skipped if from Django
import django # 1.7
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
# Your regular Django stuff here
# Init your vars etc and prepare values to debug
# The Python prompt will remain active here so you can work on it
Another less automated solution is to call execfile('test.py') or %run test.py if you're in IPython,
pro is you can reload the test.py module without leaving the shell (faster and preserves the context), con is you have to load it manually when the shell opens up
Not a direct answer, but Django Extensions (Github, Docs) shell_plus might be interesting for that. (the package has many useful tools, so it is a valuable dependency in many cases.)
The app's models are automatically imported. To configure further imports, see the additional imports section.
If you want to automatically execute code from arbitrary python modules with shell_plus, you can use the SHELL_PLUS_PRE_IMPORTS and SHELL_PLUS_POST_IMPORTS setting. Any python modules that are configured there run a) before or b) after Django's app models are auto-imported.

Automatically execute commands on launching python shell

I was wondering if there is a way to automatically run commands on entering the python shell as you would with the .bash_profile or .profile scripts with bash. I would like to automatically import some modules so I don't have to type the whole shebang everytime I hop into the shell.
Thanks,
Yup you can use the PYTHONSTARTUP environment variable to do this as outlined here
Also consider using ipython if you're doing a lot of interactive work. Your options for this kind of automation expand significantly.

Running python script inside a functional test in Django

The project that I'm recently working on load initial data by running a script:
python manage.py shell < add_initial_data.py
I'm making functional tests now and have to call this command to load initial data in the test database.
I'm trying to use subprocess.call and call_command to run that script but I can't find the option to redirect the script file to shell.
Is there a way to do that?
I've tried with
subprocess.call(['python', 'manage.py', 'shell', '<', 'add_initial_data.py'])
and
call_command('shell', '<', 'add_initial_data.py')
but gives error not recognizing < add_initial_data.py
I think you are approaching this from wrong angle. Django uses fixtures to load initial data. It is also supported in test cases django test fixture loading
If you are in python 3 and insist on your approach you might do
./manage.py shell <<EOF\ execfile('add_initial_data.py') \EOF

Categories

Resources