Initialize interpreter with variables

2019-07-22 02:12发布

问题:

How do I initialize the python interpreter such that it already has variables in its memory? For example, how could I initialize a[n i]Python interpreter, and type as my first input:

In [1]: today
Out[1]: '2015-05-05 17:49:32.726496'

without first binding the name str(today = datetime.datetime.today())?

回答1:

If you are using ipython, you can configure it to load scripts automatically for you.

Run

$ ipython profile create

which will create default profile in your home directory.

Create a file called ~/.ipython/ipython_init.py and add

import datetime
today = datetime.datetime.today

Now at the end of ~/.ipython/profile_default/ipython_config.py, add this line so that it will load that file every time interpreter starts

c.InteractiveShellApp.exec_files = ['~/.ipython/ipython_init.py']

Next time when you launch ipython shell, you can access those variables.

In [1]: today
Out[1]: datetime.datetime(2017, 3, 2, 13, 31, 26, 776744)


回答2:

You can create a script containing your "setup" code, and then execute it and enter interactive mode.

For example:

# foo.py
import datetime
today = datetime.datetime.today

Run with:

python -i foo.py
>>> today
'2015-05-05 17:49:32.726496'

I believe IPython should support the same option. The other alternative for IPython specifically is to just start it, then say:

In [1]: %run foo.py

Which will run that script in your current shell, giving you access to everything defined there.



回答3:

In addition to the other answer, you can explicitly drop into interactive mode like this:

// setup.py
import code, datetime
today = datetime.datetime.today()
code.interact(local=locals())

execute normally

python setup.py


回答4:

There are three options for the standard Python interpreter:

  • python -i setup.py, as explained in tzaman's answer
  • dropping into interactive mode from within setup.py, as explained in Jordan P's answer
  • setting the environment variable PYTHONSTARTUP=setup.py.

That last one is useful if you want to start and stop Python hundreds of times. Just export PYTHONSTARTUP=setup.py and as long as you're in the same shell, it'll always load setup.py. Or, if you want it more permanent, put it in your profile (or Windows System Control Panel Environment Variables or whatever).

PYTHONSTARTUP is especially handy with virtualenvwrapper and its post_activate hook. Just set the hook to export PYTHONSTARTUP=${VIRTUAL_ENV}/setup.py and you can have a different setup for each environment.

In fact, what -i actually does is, in effect, override PYTHONSTARTUP with a one-time temporary value.


IPython has its own very powerful (but somewhat complicated) configuration and customization system. You can build a dozen different profiles, and edit each one to enable and disable the use of -i and PYTHONSTARTUP, change PYTHONSTARTUP to use a different variable name, execute various lines of code each time a kernel is started, and so on. Most of what you want is under Terminal IPython options, if you're using it at the terminal.