How To Define Built In Function In "my" Python Interpreter?
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: def clea
Solution 1:
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()
Post a Comment for "How To Define Built In Function In "my" Python Interpreter?"