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
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.