-1

I'm looking for a method to clear python output with "replit - clear()" and "IPython.display - clear display()".

However, I encountered an error as per the attached screenshot. Can some one help me on this?

replit - clear() - error

IPython.display - clear display()- not working

Would appreciate if can help me on this.

Thanks.


Output of pip --version, python --version, python3 --version, which python and whereis python:

pip 19.1.1 from /Users/weison/anaconda3/lib/python3.7/site-packages/pip

(python 3.7)

Python 3.7.3

Python 3.9.1

/Users/weison/anaconda3/bin/python

/usr/bin/python

Executing python -m pip install -U requests in the command prompt.

Prompted Warning

Code:

from replit import clear
  

input('please enter you name:') 

clear()
print('hello')

Output:

Warning: REPLIT_DB_URL does not exist, are we running on repl.it? Database will not function.

please enter you name:weison

[H[2Jhello
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
TAN WEISON
  • 9
  • 1
  • 5
  • 1
    The [second screenshot](https://i.stack.imgur.com/mjIZ0.png) doesn't show us your `clear_output()` function's code - please attach that in order to provide the minimum reproducible code to replicate the issue. – Dbercules Feb 02 '21 at 13:22
  • Regardless, this is a duplicate issue as per [this answered question](https://stackoverflow.com/a/4810595/5568675). Please google for a solution before posting a new question. – Dbercules Feb 02 '21 at 13:30
  • tried on the alternative method: ## import os name = input ("Please enter your name: ") os.system('clear') print (f"Hello {name}, how are you?") ## Output: Please enter your name: weison Hello weison, how are you ## This is different with what i expected as i expect first will input name then clear screen, print the next. – TAN WEISON Feb 02 '21 at 13:42
  • @Dbercules He cannot provide the sources as he didn't write IPython himself ! But you can find it [on github](https://github.com/ipython/ipython/blob/af130c3f7146b1c908fc4ca4111ad7d37c6593ed/IPython/core/display_functions.py#L353) – TheEagle Feb 02 '21 at 13:47
  • Some readers might be interested to know that while Python does not have a standard method for clearing the terminal, it's been [heavily discussed](https://mail.python.org/archives/list/python-ideas@python.org/thread/EWQ2BOL3WVZAU2V2MT3HLXN3AEBHANNZ/#EWQ2BOL3WVZAU2V2MT3HLXN3AEBHANNZ) on the python-ideas mailing list in [multiple threads](https://mail.python.org/archives/list/python-ideas@python.org/thread/N2G5MDPST6IMUQCK6LLAUOVIJIOOC2XJ/#HM5DI4Z7ZLPXNITEQNJPHSCIM4DEORAD). – kojiro Feb 02 '21 at 14:44
  • @Programmer There is nothing stopping new posters from looking up the docs or source code behind functions they're using as it seems you're suggesting. We should be enabling them to be better problem solvers, and not simply providing a solution provided elsewhere numerous times before. All it takes is the encouragement of a google search. – Dbercules Feb 02 '21 at 23:05
  • @Dbercules hi mate, don't get me wrong. I already tried all the method posted in forum or available in google but still cant resolved my issue and hence ask help here. Btw, thanks for your comment and suggestion. – TAN WEISON Feb 03 '21 at 03:01

2 Answers2

0

i think there’s no built-in keyword or function/method to clear the screen. So, we do it on you own. you can do this

from os import system, name 
def clear(): 
  
    # for windows 
    if name == 'nt': 
        _ = system('cls') 
    # for mac and linux
    else: 
        _ = system('clear') 
  

print('please enter you name') 

clear()
print('hellow')

you can't use us i python display to clear console ,ipython is an interactive shell built with python.

The IPython Notebook is now known as the Jupyter Notebook. It is an interactive computational environment, in which you can combine code execution, rich text, mathematics, plots and rich media. For more details on the Jupyter Notebook

Belhadjer Samir
  • 1,461
  • 7
  • 15
-1

The replit method does not work, because it's missing a module which you can easily install by executing python -m pip install -U requests in the command prompt. The IPython one does work - it doesn't throw an error, only as far as i can see it doesn't clear the output ... And if then it always doesn't work, you can use print("\033c", end='') to clear the python console.

TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • hi Programmer, i follow your instruction install executing pip install -U requests .. yet the result still having the same error: ModuleNotFoundError: No module named 'requests' – TAN WEISON Feb 02 '21 at 13:55
  • @TANWEISON Please post the output of `pip --version`, `python --version`, `python3 --version`, `which python` and `whereis python`. – TheEagle Feb 02 '21 at 14:06
  • the output as below: pip 19.1.1 from /Users/weison/anaconda3/lib/python3.7/site-packages/pip (python 3.7) Python 3.7.3 Python 3.9.1 /Users/weison/anaconda3/bin/python /usr/bin/python – TAN WEISON Feb 02 '21 at 14:08
  • ok, then in IPython you are using Python 3.7 as one can see on the screenshot of the error, ad pip uses Python 3.9. pip using Python 3.9 will install the modules into the module folder of Python 3.9, but Python 3.7, the one you are using in IPython, looks only in its own folder. Thats why it didnt help to run pip like i suggested. I edited my answer, try the new command please – TheEagle Feb 02 '21 at 14:33
  • executed the shared pip. but promoted warning now. have edited the post with screenshot and warning message – TAN WEISON Feb 02 '21 at 15:02
  • @TANWEISON dont know how to fix this ... But you wont need that database just for clearing the terminal, i think. – TheEagle Feb 02 '21 at 15:12
  • no worries, thanks for the guide. Appreciated. – TAN WEISON Feb 02 '21 at 15:13
  • @TANWEISON i added another method for clearing the python console. if my answer helped you, accept it please. – TheEagle Feb 02 '21 at 15:19
  • it works the same.. nothing happen to clear the output.. – TAN WEISON Feb 02 '21 at 16:23
  • @TANWEISON well, maybe your IPython console just doesn't support clearing. In all cases, i have no further ideas, sorry ... – TheEagle Feb 02 '21 at 16:34
  • thanks for you patience and all the info share. – TAN WEISON Feb 02 '21 at 16:40
  • @TANWEISON but you could try to open the python interpreter in a normal command prompt, and execute `print("\033c", end='')` there. If that works, you know the IPython shell jsut doesn't support clearing. For the IPython prompt you can use `print("\n" * 100)` (printing 100 newlines is guaranteed to work, and guaranteed to clear the screen) – TheEagle Feb 02 '21 at 19:02