Để xây dựng trên câu trả lời của IVA: embedding-a-shell, incoporating code
và ipython.
def prompt(vars=None, message="welcome to the shell"):
#prompt_message = "Welcome! Useful: G is the graph, DB, C"
prompt_message = message
try:
from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed(argv=[''],banner=prompt_message,exit_msg="Goodbye")
return ipshell
except ImportError:
if vars is None: vars=globals()
import code
import rlcompleter
import readline
readline.parse_and_bind("tab: complete")
# calling this with globals ensures we can see the environment
print prompt_message
shell = code.InteractiveConsole(vars)
return shell.interact
p = prompt()
p()
Nguồn
2010-04-12 04:15:31
Cảm ơn tất cả! Đối với hồ sơ, cách dễ nhất để sử dụng mô-đun mã để đạt được điều này là: mã nhập mã.interact (local = globals()) –
Để nhận biến cục bộ vào không gian tên, bạn cần ' code.interact (local = dict (globals(), ** locals()) '. Lưu ý việc thêm' ** locals'. Tôi tự hỏi câu hỏi này và nhận xét của bạn là câu trả lời hay nhất tôi tìm thấy:) –