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

Is there a way to store commands in Python?

For example, to save the bash command, I can put:

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

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

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

which will work on the python command line whenever / when it opens? Thanks.

+6
source share
4 answers

In Bash, I assume that you define these aliases in a .profile , .bash_rc or similar file. In this file add the line

 export PYTHONSTARTUP=~/.python_rc.py 

This will allow you to create a .python_rc.py file that will be included each time the session starts at the Python / REPL prompt. (It will not be included when running Python scripts, because it can be destructive for this).

Inside this file, you can define a function for the command that you want to save. In your case, what you are doing is actually more complicated than it sounds, so you will 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 that, you can call profile() to import the Profile at the Python prompt.

+5
source

I would recommend using IPython , it surpasses the standard interpreter in many ways, and in this specific case you can use the 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 will be a valid multi-line macro. The Django manage.py shell team will automatically use IPython, if available.

+4
source

Kinda

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

Create a shell script that runs the Python interpreter as follows:

 python -i myprofile.py 

When executing the shell script, it will execute the myprofile.py file and run the interpreter subsequently.

So, if you have myprofile.py file:

 def do_stuff(x): print(x) 

And ran your parenthesis script ", you could do:

 >>> do_stuff(1) 1 
+1
source

Do not use exec, this is bad and wrong

However, I think you need him to do what you need.

  • Create a Python script. Add lines to it, e.g.

     # pythonprofile.py profile = "from userprofile.models import Profile" 
  • Create the PYTHONSTARTUP environment variable pointing to the script. This will cause the code to run in the interpreter at startup.

  • Then, to actually use the do command

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

Executes the code contained in the profile line in the current area. exec dangerous, so be careful.

Edit: @Jeremy's solution is good, but it requires writing more code for an alias than this method; either works.

+1
source

Source: https://habr.com/ru/post/895511/


All Articles