How to disable the interactive mode automatic reload module during python development

Source: Internet
Author: User
Tags versions

Anyone who wants to develop python, especially django, will have an experience: Entering the python interaction mode (directly executing the python Press enter) or entering the django-shell debugging function, and then modifying the source code, exit the interaction mode or djangoshell, and re-enter those modules in one-to-one import... What is the problem? It's a waste of time. Why don't I modify the source code auto reload like the web framework?

I spent more than two weeks doing this. In fact, the principle is to encapsulate ipython to my shell, and then add this autoreload function to my shell. But I saw the source code of ipython last night and found that: ipython has already been implemented...

The source code file of my system implementation is/usr/lib64/python2.7/site-packages/IPython/Extensions/ipy_autoreload.py.

Implementation in ipython interactive mode

It is related to the ipython version, and is loaded in versions later than 0.11.

% Load_ext autoreload
% Autoreload 2

If the value is less than 0.11, <div class = 'bogus-wrapper '>
<Pre class = 'sh _ '> import ipy_autoreload % autoreload 2 </pre>
</Div>

Automatic loading in ipython interactive mode

You don't need to execute these two statements every time you enter ipython, so you can add them to the custom configuration of ipython. because the operating system and ipython versions are different, the user-defined directories of ipython are different, and the added configurations are also different.

First create ipython personal configuration

Ipython profile create

Gentoo ~ /. Ipython: 0.10.2

The configuration file is. ipython/ipy_user_conf.py added

Import ipy_autoreload
O. autoexec. append ('% autoreload 2 ')

Opensuse ~ /. Config/ipython: 0.13

The configuration file is ~ /. Config/ipython/profile_default/ipython_config.py

C.InteractiveShellApp.exe c_lines.append ('% load_ext autoreload ')
C.InteractiveShellApp.exe c_lines.append ('% autoreload 2 ')

Django shell implementation

Unfortunately, we changed core/management/commands/shell in django source code. py, does not provide automatic reload, but when you modify the ipython configuration, it will also work (this OS is related, I will explain below the geek method does not work)

PS: You can also use shell_plus.py in django-extensions.
When your system has ipython, bpython, and default python, the order of django shell selection

If your system does not have ipython or bpython, the default python

The shell_plus traversal list is bpython-> ipython.

The traversal sequence of the built-in shell of django is ipython-> bpython

What should I do if djangoshell does not work? First, I read the django/ipython source code (0.13.2). In fact, there is no problem with shell_plus in djangoshell or even django-extensions. The key is ipython.

I only mean that the external modules in the user configuration are not correctly executed.

The process of ipython entering the interaction mode

When ipython is called, it is started through the launch_new_instance function of IPython. frontend. terminal. ipapp.
Use the init_code method in the InteractiveShellApp class of IPython. core. shellapp to initialize loading after startup.
In the init_code method, self. _ run_exec_lines () will be executed. This is the import and execution of the above module.

The process for djangoshell to enter the interaction mode

Start with the TerminalInteractiveShell class of IPython. frontend. terminal. embed.
In TerminalInteractiveShell initialization, the above third entry is not executed

The solution is to forcibly modify the source code of IPython/frontend/terminal/interactiveshell. py to add the module initialization operation to him.

Add a section at the beginning of line 1 (containing the existing code helps you understand where to add the code, that is, the following lines 3-8)

Self. init_usage (usage)
Self. init_banner (banner1, banner2, display_banner)
Exex_lines = self. config ['interactiveshellapp'] ['exec _ Lines']
For line in exex_lines:
Try:
Self. run_cell (line, store_history = False)
Except t:
Pass
#-------------------------------------------------------------------------
# Overrides of init stages
#-------------------------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.