The shell sub-command simply invokes an interactive Python interpreter, so pointing the PYTHONSTARTUP UNIX environment variable to a file containing your desired imports would work. Here is the sequence:
user@linux$ export PYTHONSTARTUP='/path/to/my/django/pythonStartup.py'; python ./manage.py shell
Where pythonStartup.py is arbitrarily named and you can name it anything you like, including s.py (although that is probably not the best name for it). =:)
You can also create the following convenience alias for that in your personal .bash_profile:
alias django-shell="export PYTHONSTARTUP='/path/to/my/django/pythonStartup.py'; python ./manage.py shell"
and then simply use that:
user@linux$ . ${HOME}/.bash_profile # Normally you don't need to do this step.
user@linux$ django-shell
Now, you need only to edit the pythonStartup.py file to incorporate any changes to import behavior you might desire, and simply run the alias (... no need to edit or re-source your .bash_profile).
Here is what happens when I run python3 ./manage.py shell with the PYTHONSTARTUP environment variable properly pointed to the file I want imports to come from:
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's features.
%quickref -> Quick reference.
help -> Python's 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:
An additional tip that I forgot to mention.
If you place pythonStartup.py in the root directory of your Django projects, then creating the alias as follows:
alias django-shell="export PYTHONSTARTUP='./pythonStartup.py'; python ./manage.py shell"
allows you to cd to the root directory of whatever Django project you're currently working on, and the alias will invoke that particular project's pythonStartup.py. This approach adds flexibility.