1

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 clear():
  import os
  os.system('clear')

And run python in shell

Then I type clear() and the screen gets cleared without having to define clear each time I invoke python interpreter.

I am interested in a solution that fits Ubuntu or any Linux distribution. however, any solution for any platform is welcome.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Myonaiz
  • 326
  • 5
  • 14
  • thanks for your reply. is there a way that this personal library gets loaded by default whenever I start python? – Myonaiz Jul 15 '17 at 15:47
  • If you just want clear, then why don't you use iPython interpreter instead of the default repl? Try it out. You will thank me later :) – Banach Tarski Jul 15 '17 at 15:49
  • @cricket_007 thanks a lot. you answered my question. – Myonaiz Jul 15 '17 at 15:50
  • @BanachTarski though it is not just clear I want, but I will try it. thanks a lot ^_^ – Myonaiz Jul 15 '17 at 15:51
  • I'd actually say, if you're using python repl, and ipython, you might as well go big and go with an ipython/jupyter notebook – Kelvin Jul 15 '17 at 15:52
  • 2
    Voting to re-open. The duplicate does not answer the question. The question is not about ipython nor numpy. Using User Customisation from https://docs.python.org/3/tutorial/appendix.html#the-customization-modules is the generic way to do this – Alastair McCormack Jul 15 '17 at 15:57

1 Answers1

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()
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100