开发者

How can I permanently store commands in the Python REPL/prompt?

Is there a way to store commands in Python?

For example, to store a bash command I can put:

# in .bash_profile
alias myproject="cd /path/to/my/project"

$ project

Is there a way to store a command, for example something like this:

'store' profile="from userprofile.models import Profile"

>>> profile

which will work in the Python command prompt whenever/wh开发者_JAVA百科erever it is opened? Thank you.


In Bash, I'm assuming you are defining this aliases in .profile, .bash_rc or a similar file. In that file, add the line

export PYTHONSTARTUP=~/.python_rc.py

This will allow you to create a .python_rc.py file that is included whenever you start a session in the Python prompt/REPL. (It will not be included when running Python scripts, becasue it could be disruptive to do so.)

Inside that file, you could define a function for the command you want to save. In your case what you're doing is actually a touch more complicated than it seems, so you'd need to use a few more lines:

def profile():
    global Profile
    import sys
    if "path/to/your/project" not in sys.path:
        sys.path.append("path/to/your/project")
    from userprofile.models import Profile

After doing this, you'll be able to call profile() to import Profile in the Python prompt.


I'd recommend using IPython, it's superior to the standard interpreter in a lot of ways, and in this particular case you can take advantage of it's ability to save macros:

In [1]: from userprofile.models import Profile

In [2]: macro profile 1 # profile being the name of the macro, 1 being the line to use
Macro `profile` created. To execute, type its name (without quotes).
=== Macro contents: ===
from userprofile.models import Profile

In [3]: profile # you can now use your macro

Macros can also span multiple lines, macro some_macro 11 13 would be a valid multiline macro. Django's manage.py shell command will automatically use IPython if it's available.


Kinda.

Write your "profile" as a script and save it somewhere.

Create a shell script that executes the Python interpreter like this:

python -i myprofile.py

When you execute the shell script it will execute the file myprofile.py and start the interpreter afterwards.

So if you had a file myprofile.py:

def do_stuff(x):
    print(x)

And ran your shell script "shortcut", you could do:

>>> do_stuff(1)
1


Don't use exec, it's bad and wrong

However, I think you need it to do what you want.

  1. Create a Python script. Add lines to it like

    # pythonprofile.py
    profile = "from userprofile.models import Profile"
    
  2. Create a PYTHONSTARTUP environmental variable pointing to the script. This will cause the code to be executed in the interpreter when it starts up.

  3. Then to actually use the command do

    exec(profile) # Don't ever do this with code you don't trust. 
    

This executes the code contained in the string profile in the current scope. exec is dangerous, so be careful doing this.

Edit: @Jeremy's solution is good, but it requires you to write more code per alias than this method; either one works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜