How to define built in function in “my” python interpreter?

前端 未结 1 358

I would like to add a python function in my interpreter that exists by default whenever I run python in my shell. The scenario I want is:

write a function like this once

相关标签:
1条回答
  • 2021-01-23 18:15

    Use Customisation Modules: https://docs.python.org/3/tutorial/appendix.html#the-customization-modules

    With it, you can run code and define methods that are available when Python starts up interactively.

    First, find where Python is looking for the user customisation directory is. Start python and type:

    >>> import site
    >>> site.getusersitepackages()
    '/home/user/myuser/.local/lib/python3.5/site-packages'
    

    The last string given is the customisation directory. If it does not exist, create it. Then create a file in your customisation directory called usercustomize.py. Add your code from the question to it. Save and restart Python.

    Voila! You can now type:

    >>> clear()
    
    0 讨论(0)
提交回复
热议问题