How to configure Django manage.py shell?

Almost every time I use the manage.py shell in Django, I want to import certain things. For example, I want to import * from my models.py module. My current job is to put all the imports into a file called s.py, and then after running the shell, I type execfile ('s.py').

How can I configure manage.py to automatically import imports when the shell starts? I am using Django 1.4. Thanks.

Edit: I am adding more details to make my question more understandable.

Here is what I originally did:

bash> python manage.py shell >>> from mysite.app.models import * >>> from mysite.app2.models import * >>> import decimal >>> D = decimal.Decimal # Now I am ready to work in this shell. 

Entering 4 lines of the template each time the shell starts is annoying. So I put these 4 lines in the s.py file. Now I do this:

 bash> python manage.py shell >>> execfile('s.py') # Now I am ready to work. 

I want to get rid of execfile('s.py') too. I want manage.py do these imports automatically when the shell starts.

+4
source share
4 answers

I think you should not override the default shell commands or manage.py because you intend to use them elsewhere. If you have a local settings file, you can add them to your local settings file, but you can get circular imports if you are not careful. You should also write your own shell extension

Check this:

 https://github.com/django-extensions/django-extensions/blob/master/django_extensions/management/shells.py 
0
source

The shell subcommand simply calls the interactive Python interpreter, so specifying the PYTHONSTARTUP UNIX environment variable in the file containing the required import will work. Here is the sequence:

 user@linux $ export PYTHONSTARTUP='/path/to/my/django/pythonStartup.py'; python ./manage.py shell 

Where pythonStartup.py has an arbitrary name, and you can call it anything, including s.py (although this is probably not the best name for it). = :)

You can also create the following convenience alias for your personal .bash_profile :

 alias django-shell="export PYTHONSTARTUP='/path/to/my/django/pythonStartup.py'; python ./manage.py shell" 

and then just use this:

 user@linux $ . ${HOME}/.bash_profile # Normally you don't need to do this step. user@linux $ django-shell 

Now you only need to edit the pythonStartup.py file to include any changes to the import behavior that you may need and just run the alias (... there is no need to edit or rename your .bash_profile ).

This is what happens when I run python3./manage.py shell with the PYTHONSTARTUP environment variable, which correctly points to the file I want to import into:

 user@linux $ python3 ./manage.py shell Python 3.5.1 |Anaconda custom (64-bit)| (default, Dec 7 2015, 11:16:01) Type "copyright", "credits" or "license" for more information. IPython 4.2.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython features. %quickref -> Quick reference. help -> Python own help system. object? -> Details about 'object', use 'object??' for extra details. Importing base set of modules often used ... import sys, os, random, pprint, operator import time, math import numpy, numpy as np import numpy.linalg import scipy, scipy as spimport scipy.optimize import matplotlib import matplotlib.pyplot as plt import matplotlib.pylab as pylab import pandas as pd import sklearn.datasets import sklearn.feature_extraction import sklearn.linear_model import sklearn.neighbors import sklearn.cluster import sklearn.preprocessing import sklearn.decomposition import gensim.models.word2vec In [1]: 

EDIT:

Extra tip that I forgot to mention.

If you place pythonStartup.py in the root directory of your Django projects, create an alias as follows:

 alias django-shell="export PYTHONSTARTUP='./pythonStartup.py'; python ./manage.py shell" 

allows cd to the root directory of any Django project you are currently working on, and an alias will call this particular pythonStartup.py project. This approach increases flexibility.

0
source

Check django-extensions , it provides a shell_plus control shell_plus that automatically imports all models for installed applications:

 user@host :~/git/project (devel)$ ./manage.py shell_plus # Shell Plus Model Imports from django.contrib.admin.models import LogEntry from django.contrib.auth.models import Group, Permission, User from django.contrib.contenttypes.models import ContentType from django.contrib.sessions.models import Session from custom_app1.models import MyModel1 from custom_app2.models import MyModel2 from custom_app3.models import MyModel3 # Shell Plus Django Imports from django.utils import timezone from django.conf import settings from django.core.cache import cache from django.db.models import Avg, Count, F, Max, Min, Sum, Q, Prefetch, Case, When from django.core.urlresolvers import reverse from django.db import transaction Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) 

Please note that custom application models are also imported.

0
source

There is a django extension for you, shell_plus .

1 . pip install django-extensions

2 . add 'django-extensions' to your settings.py INSTALLED_APPS []

3 . RUN COMMAND → →> python manage.py shell_plus

0
source

Source: https://habr.com/ru/post/1487968/


All Articles