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.

6
  • thanks for your reply. is there a way that this personal library gets loaded by default whenever I start python? Commented Jul 15, 2017 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 :) Commented Jul 15, 2017 at 15:49
  • @cricket_007 thanks a lot. you answered my question. Commented Jul 15, 2017 at 15:50
  • @BanachTarski though it is not just clear I want, but I will try it. thanks a lot ^_^ Commented Jul 15, 2017 at 15:51
  • 2
    Voting to re-open. The duplicate does not answer the question. The question is not about ipython nor numpy. Using User Customisation from docs.python.org/3/tutorial/… is the generic way to do this Commented Jul 15, 2017 at 15:57

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.