How can I disable the new history feature in Python 3.4?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
3












Since upgrading to Python 3.4, all interactive commands are logged to ~/.python_history. I don't want Python to create or write to this file.



Creating a symlink to /dev/null does not work, Python removes the file and recreates it. The documentation suggests to delete the sys.__interactivehook__, but this also removes tab-completion. What should be done to disable writing this history file but still preserve tab-completion?



Additional details:



  • Distro: Arch Linux x86_64

  • readline 6.3-3

  • python 3.4.0-2









share|improve this question























  • Have you tried chowning it to root?
    – goldilocks
    Mar 25 '14 at 15:45











  • @goldilocks I do not consider that an acceptable solution, and neither is using chattr. I am looking for a single (user) configuration file or environment variable.
    – Lekensteyn
    Mar 25 '14 at 15:47










  • Okay, but you already know there is no such acceptable solution since you've looked at the documentation.
    – goldilocks
    Mar 25 '14 at 15:50










  • In fact, chmod is the solution suggested by python devs in this bug report from a few weeks ago, although there is also some mention of ~/.inputrc there (there is no man readline, BTW, except for the library function).
    – goldilocks
    Mar 25 '14 at 15:55











  • @goldilocks Yes, I was about to link to that bug report. I don't know how to use inputrc though and chmod does not work for me either, the file gets modified anyway (Arch Linux). There exists an info inputrc page, but I am generally not so happy with reading info pages as they are difficult to navigate through.
    – Lekensteyn
    Mar 25 '14 at 15:56















up vote
5
down vote

favorite
3












Since upgrading to Python 3.4, all interactive commands are logged to ~/.python_history. I don't want Python to create or write to this file.



Creating a symlink to /dev/null does not work, Python removes the file and recreates it. The documentation suggests to delete the sys.__interactivehook__, but this also removes tab-completion. What should be done to disable writing this history file but still preserve tab-completion?



Additional details:



  • Distro: Arch Linux x86_64

  • readline 6.3-3

  • python 3.4.0-2









share|improve this question























  • Have you tried chowning it to root?
    – goldilocks
    Mar 25 '14 at 15:45











  • @goldilocks I do not consider that an acceptable solution, and neither is using chattr. I am looking for a single (user) configuration file or environment variable.
    – Lekensteyn
    Mar 25 '14 at 15:47










  • Okay, but you already know there is no such acceptable solution since you've looked at the documentation.
    – goldilocks
    Mar 25 '14 at 15:50










  • In fact, chmod is the solution suggested by python devs in this bug report from a few weeks ago, although there is also some mention of ~/.inputrc there (there is no man readline, BTW, except for the library function).
    – goldilocks
    Mar 25 '14 at 15:55











  • @goldilocks Yes, I was about to link to that bug report. I don't know how to use inputrc though and chmod does not work for me either, the file gets modified anyway (Arch Linux). There exists an info inputrc page, but I am generally not so happy with reading info pages as they are difficult to navigate through.
    – Lekensteyn
    Mar 25 '14 at 15:56













up vote
5
down vote

favorite
3









up vote
5
down vote

favorite
3






3





Since upgrading to Python 3.4, all interactive commands are logged to ~/.python_history. I don't want Python to create or write to this file.



Creating a symlink to /dev/null does not work, Python removes the file and recreates it. The documentation suggests to delete the sys.__interactivehook__, but this also removes tab-completion. What should be done to disable writing this history file but still preserve tab-completion?



Additional details:



  • Distro: Arch Linux x86_64

  • readline 6.3-3

  • python 3.4.0-2









share|improve this question















Since upgrading to Python 3.4, all interactive commands are logged to ~/.python_history. I don't want Python to create or write to this file.



Creating a symlink to /dev/null does not work, Python removes the file and recreates it. The documentation suggests to delete the sys.__interactivehook__, but this also removes tab-completion. What should be done to disable writing this history file but still preserve tab-completion?



Additional details:



  • Distro: Arch Linux x86_64

  • readline 6.3-3

  • python 3.4.0-2






python interactive






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 '14 at 9:15

























asked Mar 25 '14 at 15:38









Lekensteyn

9,580115086




9,580115086











  • Have you tried chowning it to root?
    – goldilocks
    Mar 25 '14 at 15:45











  • @goldilocks I do not consider that an acceptable solution, and neither is using chattr. I am looking for a single (user) configuration file or environment variable.
    – Lekensteyn
    Mar 25 '14 at 15:47










  • Okay, but you already know there is no such acceptable solution since you've looked at the documentation.
    – goldilocks
    Mar 25 '14 at 15:50










  • In fact, chmod is the solution suggested by python devs in this bug report from a few weeks ago, although there is also some mention of ~/.inputrc there (there is no man readline, BTW, except for the library function).
    – goldilocks
    Mar 25 '14 at 15:55











  • @goldilocks Yes, I was about to link to that bug report. I don't know how to use inputrc though and chmod does not work for me either, the file gets modified anyway (Arch Linux). There exists an info inputrc page, but I am generally not so happy with reading info pages as they are difficult to navigate through.
    – Lekensteyn
    Mar 25 '14 at 15:56

















  • Have you tried chowning it to root?
    – goldilocks
    Mar 25 '14 at 15:45











  • @goldilocks I do not consider that an acceptable solution, and neither is using chattr. I am looking for a single (user) configuration file or environment variable.
    – Lekensteyn
    Mar 25 '14 at 15:47










  • Okay, but you already know there is no such acceptable solution since you've looked at the documentation.
    – goldilocks
    Mar 25 '14 at 15:50










  • In fact, chmod is the solution suggested by python devs in this bug report from a few weeks ago, although there is also some mention of ~/.inputrc there (there is no man readline, BTW, except for the library function).
    – goldilocks
    Mar 25 '14 at 15:55











  • @goldilocks Yes, I was about to link to that bug report. I don't know how to use inputrc though and chmod does not work for me either, the file gets modified anyway (Arch Linux). There exists an info inputrc page, but I am generally not so happy with reading info pages as they are difficult to navigate through.
    – Lekensteyn
    Mar 25 '14 at 15:56
















Have you tried chowning it to root?
– goldilocks
Mar 25 '14 at 15:45





Have you tried chowning it to root?
– goldilocks
Mar 25 '14 at 15:45













@goldilocks I do not consider that an acceptable solution, and neither is using chattr. I am looking for a single (user) configuration file or environment variable.
– Lekensteyn
Mar 25 '14 at 15:47




@goldilocks I do not consider that an acceptable solution, and neither is using chattr. I am looking for a single (user) configuration file or environment variable.
– Lekensteyn
Mar 25 '14 at 15:47












Okay, but you already know there is no such acceptable solution since you've looked at the documentation.
– goldilocks
Mar 25 '14 at 15:50




Okay, but you already know there is no such acceptable solution since you've looked at the documentation.
– goldilocks
Mar 25 '14 at 15:50












In fact, chmod is the solution suggested by python devs in this bug report from a few weeks ago, although there is also some mention of ~/.inputrc there (there is no man readline, BTW, except for the library function).
– goldilocks
Mar 25 '14 at 15:55





In fact, chmod is the solution suggested by python devs in this bug report from a few weeks ago, although there is also some mention of ~/.inputrc there (there is no man readline, BTW, except for the library function).
– goldilocks
Mar 25 '14 at 15:55













@goldilocks Yes, I was about to link to that bug report. I don't know how to use inputrc though and chmod does not work for me either, the file gets modified anyway (Arch Linux). There exists an info inputrc page, but I am generally not so happy with reading info pages as they are difficult to navigate through.
– Lekensteyn
Mar 25 '14 at 15:56





@goldilocks Yes, I was about to link to that bug report. I don't know how to use inputrc though and chmod does not work for me either, the file gets modified anyway (Arch Linux). There exists an info inputrc page, but I am generally not so happy with reading info pages as they are difficult to navigate through.
– Lekensteyn
Mar 25 '14 at 15:56











7 Answers
7






active

oldest

votes

















up vote
4
down vote













As of Python 3.6, you can use readline.set_auto_history to disable this:



import readline
readline.set_auto_history(False)





share|improve this answer



























    up vote
    3
    down vote













    This works for me.



    Creating ~/.pythonrc file:



    import os
    import atexit
    import readline

    readline_history_file = os.path.join(os.path.expanduser('~'), '.python_history')
    try:
    readline.read_history_file(readline_history_file)
    except IOError:
    pass

    readline.set_history_length(0)
    atexit.register(readline.write_history_file, readline_history_file)


    Then export it:



    export PYTHONSTARTUP=~/.pythonrc





    share|improve this answer






















    • This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
      – Lekensteyn
      Mar 25 '14 at 16:52











    • @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
      – cuonglm
      Mar 25 '14 at 16:54







    • 1




      This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
      – Lekensteyn
      Mar 25 '14 at 16:58










    • AFAIK, there is no way to really disable it.
      – cuonglm
      Mar 25 '14 at 17:06

















    up vote
    0
    down vote













    Until it's fixed in some way in Python itself you can do this on UNIX systems:



    rm ~/.python-history
    mkdir ~/.python-history
    sudo chattr +i ~/.python-history || sudo chflags simmutable ~/.python-history


    After that you will be getting




    Error in atexit._run_exitfuncs:



    IsADirectoryError: [Errno 21] Is a directory




    every time you terminate a python shell. Exit status will still be 0.



    Notice that if you leave it as file you need to create and make immutable another file, ~/.python_history






    share|improve this answer





























      up vote
      0
      down vote













      Put the following in a file and set PYTHONSTARTUP to it (or call the file sitecustomize.py and make it accessible from the PYTHONPATH)



      import readline
      import atexit
      import sys

      sys.__interactivehook__()
      atexit.unregister(readline.write_history_file)


      This way you'll still have access to tab-completion and previous history, but the commands you enter won't be added to the history file.






      share|improve this answer




















      • This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
        – Lekensteyn
        Feb 9 '16 at 14:39










      • I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
        – berdario
        Feb 10 '16 at 0:54

















      up vote
      0
      down vote



      accepted










      To prevent Python from writing ~/.python_history, disable the hook that activates this functionality:



      import sys
      # Disable history (...but also auto-completion :/ )
      if hasattr(sys, '__interactivehook__'):
      del sys.__interactivehook__


      If you would like to enable tab-completion and disable the history feature, you can adapt the site.enablerlcompleter code. Write the following code to ~/.pythonrc and set export PYTHONSTARTUP=~/.pythonrc in your shell to enable it.



      import sys
      def register_readline_completion():
      # rlcompleter must be loaded for Python-specific completion
      try: import readline, rlcompleter
      except ImportError: return
      # Enable tab-completion
      readline_doc = getattr(readline, '__doc__', '')
      if readline_doc is not None and 'libedit' in readline_doc:
      readline.parse_and_bind('bind ^I rl_complete')
      else:
      readline.parse_and_bind('tab: complete')
      sys.__interactivehook__ = register_readline_completion





      share|improve this answer



























        up vote
        0
        down vote













        Another ~/.pythonrc solution:



        import readline
        readline.write_history_file = lambda *args: None





        share|improve this answer



























          up vote
          0
          down vote













          Here's my method (which turned out to be basically a more reliable and pythonic version of berdario's method). It only disables writing to .python_history, but not reading from it if it exists, or appending new lines to the current instance's history. I recommend saving it as site-packages/sitecustomize.py, because site is the module that both writes to .python_history and imports sitecustomize if it exists, although naming it something else and pointing to it with PYTHONSTARTUP works too.



          import sys
          oldhook = getattr(sys, '__interactivehook__', None)
          if oldhook:
          def newhook():
          import readline, atexit
          oldhook()
          atexit.unregister(readline.write_history_file)
          sys.__interactivehook__ = newhook


          update: what i did for 3.7, specific to my system, not pep8:



          import rlcompleter,readline as r,sys
          def f():r.parse_and_bind('tab:complete');r.read_init_file()
          sys.__interactivehook__=f





          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f121377%2fhow-can-i-disable-the-new-history-feature-in-python-3-4%23new-answer', 'question_page');

            );

            Post as a guest






























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            As of Python 3.6, you can use readline.set_auto_history to disable this:



            import readline
            readline.set_auto_history(False)





            share|improve this answer
























              up vote
              4
              down vote













              As of Python 3.6, you can use readline.set_auto_history to disable this:



              import readline
              readline.set_auto_history(False)





              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                As of Python 3.6, you can use readline.set_auto_history to disable this:



                import readline
                readline.set_auto_history(False)





                share|improve this answer












                As of Python 3.6, you can use readline.set_auto_history to disable this:



                import readline
                readline.set_auto_history(False)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 9 at 16:48









                Colin Watson

                2,534139




                2,534139






















                    up vote
                    3
                    down vote













                    This works for me.



                    Creating ~/.pythonrc file:



                    import os
                    import atexit
                    import readline

                    readline_history_file = os.path.join(os.path.expanduser('~'), '.python_history')
                    try:
                    readline.read_history_file(readline_history_file)
                    except IOError:
                    pass

                    readline.set_history_length(0)
                    atexit.register(readline.write_history_file, readline_history_file)


                    Then export it:



                    export PYTHONSTARTUP=~/.pythonrc





                    share|improve this answer






















                    • This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
                      – Lekensteyn
                      Mar 25 '14 at 16:52











                    • @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
                      – cuonglm
                      Mar 25 '14 at 16:54







                    • 1




                      This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
                      – Lekensteyn
                      Mar 25 '14 at 16:58










                    • AFAIK, there is no way to really disable it.
                      – cuonglm
                      Mar 25 '14 at 17:06














                    up vote
                    3
                    down vote













                    This works for me.



                    Creating ~/.pythonrc file:



                    import os
                    import atexit
                    import readline

                    readline_history_file = os.path.join(os.path.expanduser('~'), '.python_history')
                    try:
                    readline.read_history_file(readline_history_file)
                    except IOError:
                    pass

                    readline.set_history_length(0)
                    atexit.register(readline.write_history_file, readline_history_file)


                    Then export it:



                    export PYTHONSTARTUP=~/.pythonrc





                    share|improve this answer






















                    • This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
                      – Lekensteyn
                      Mar 25 '14 at 16:52











                    • @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
                      – cuonglm
                      Mar 25 '14 at 16:54







                    • 1




                      This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
                      – Lekensteyn
                      Mar 25 '14 at 16:58










                    • AFAIK, there is no way to really disable it.
                      – cuonglm
                      Mar 25 '14 at 17:06












                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    This works for me.



                    Creating ~/.pythonrc file:



                    import os
                    import atexit
                    import readline

                    readline_history_file = os.path.join(os.path.expanduser('~'), '.python_history')
                    try:
                    readline.read_history_file(readline_history_file)
                    except IOError:
                    pass

                    readline.set_history_length(0)
                    atexit.register(readline.write_history_file, readline_history_file)


                    Then export it:



                    export PYTHONSTARTUP=~/.pythonrc





                    share|improve this answer














                    This works for me.



                    Creating ~/.pythonrc file:



                    import os
                    import atexit
                    import readline

                    readline_history_file = os.path.join(os.path.expanduser('~'), '.python_history')
                    try:
                    readline.read_history_file(readline_history_file)
                    except IOError:
                    pass

                    readline.set_history_length(0)
                    atexit.register(readline.write_history_file, readline_history_file)


                    Then export it:



                    export PYTHONSTARTUP=~/.pythonrc






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 25 '14 at 16:51

























                    answered Mar 25 '14 at 16:38









                    cuonglm

                    99.5k23194292




                    99.5k23194292











                    • This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
                      – Lekensteyn
                      Mar 25 '14 at 16:52











                    • @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
                      – cuonglm
                      Mar 25 '14 at 16:54







                    • 1




                      This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
                      – Lekensteyn
                      Mar 25 '14 at 16:58










                    • AFAIK, there is no way to really disable it.
                      – cuonglm
                      Mar 25 '14 at 17:06
















                    • This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
                      – Lekensteyn
                      Mar 25 '14 at 16:52











                    • @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
                      – cuonglm
                      Mar 25 '14 at 16:54







                    • 1




                      This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
                      – Lekensteyn
                      Mar 25 '14 at 16:58










                    • AFAIK, there is no way to really disable it.
                      – cuonglm
                      Mar 25 '14 at 17:06















                    This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
                    – Lekensteyn
                    Mar 25 '14 at 16:52





                    This seems to enable writing a history file instead of disabling it if I am not mistaken? Unless set_history_length does something magical? Tab-completion is done for functions, not history.
                    – Lekensteyn
                    Mar 25 '14 at 16:52













                    @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
                    – cuonglm
                    Mar 25 '14 at 16:54





                    @Lekensteyn: yes, it make history size equal zero so nothing is written to history file. Oh, I though you want to preserve command between sessions.
                    – cuonglm
                    Mar 25 '14 at 16:54





                    1




                    1




                    This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
                    – Lekensteyn
                    Mar 25 '14 at 16:58




                    This still causes ~/.python_history to be written (verified with PYTHONSTARTUP=$HOME/.pythonrc strace -e file,write -o /tmp/st python). I start to think that there is no way to disable this, but keep tab-completion without duplicating code from /usr/lib/python3.4/site.py.
                    – Lekensteyn
                    Mar 25 '14 at 16:58












                    AFAIK, there is no way to really disable it.
                    – cuonglm
                    Mar 25 '14 at 17:06




                    AFAIK, there is no way to really disable it.
                    – cuonglm
                    Mar 25 '14 at 17:06










                    up vote
                    0
                    down vote













                    Until it's fixed in some way in Python itself you can do this on UNIX systems:



                    rm ~/.python-history
                    mkdir ~/.python-history
                    sudo chattr +i ~/.python-history || sudo chflags simmutable ~/.python-history


                    After that you will be getting




                    Error in atexit._run_exitfuncs:



                    IsADirectoryError: [Errno 21] Is a directory




                    every time you terminate a python shell. Exit status will still be 0.



                    Notice that if you leave it as file you need to create and make immutable another file, ~/.python_history






                    share|improve this answer


























                      up vote
                      0
                      down vote













                      Until it's fixed in some way in Python itself you can do this on UNIX systems:



                      rm ~/.python-history
                      mkdir ~/.python-history
                      sudo chattr +i ~/.python-history || sudo chflags simmutable ~/.python-history


                      After that you will be getting




                      Error in atexit._run_exitfuncs:



                      IsADirectoryError: [Errno 21] Is a directory




                      every time you terminate a python shell. Exit status will still be 0.



                      Notice that if you leave it as file you need to create and make immutable another file, ~/.python_history






                      share|improve this answer
























                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        Until it's fixed in some way in Python itself you can do this on UNIX systems:



                        rm ~/.python-history
                        mkdir ~/.python-history
                        sudo chattr +i ~/.python-history || sudo chflags simmutable ~/.python-history


                        After that you will be getting




                        Error in atexit._run_exitfuncs:



                        IsADirectoryError: [Errno 21] Is a directory




                        every time you terminate a python shell. Exit status will still be 0.



                        Notice that if you leave it as file you need to create and make immutable another file, ~/.python_history






                        share|improve this answer














                        Until it's fixed in some way in Python itself you can do this on UNIX systems:



                        rm ~/.python-history
                        mkdir ~/.python-history
                        sudo chattr +i ~/.python-history || sudo chflags simmutable ~/.python-history


                        After that you will be getting




                        Error in atexit._run_exitfuncs:



                        IsADirectoryError: [Errno 21] Is a directory




                        every time you terminate a python shell. Exit status will still be 0.



                        Notice that if you leave it as file you need to create and make immutable another file, ~/.python_history







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 13 '14 at 19:08

























                        answered Oct 13 '14 at 18:54









                        int_ua

                        1637




                        1637




















                            up vote
                            0
                            down vote













                            Put the following in a file and set PYTHONSTARTUP to it (or call the file sitecustomize.py and make it accessible from the PYTHONPATH)



                            import readline
                            import atexit
                            import sys

                            sys.__interactivehook__()
                            atexit.unregister(readline.write_history_file)


                            This way you'll still have access to tab-completion and previous history, but the commands you enter won't be added to the history file.






                            share|improve this answer




















                            • This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
                              – Lekensteyn
                              Feb 9 '16 at 14:39










                            • I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
                              – berdario
                              Feb 10 '16 at 0:54














                            up vote
                            0
                            down vote













                            Put the following in a file and set PYTHONSTARTUP to it (or call the file sitecustomize.py and make it accessible from the PYTHONPATH)



                            import readline
                            import atexit
                            import sys

                            sys.__interactivehook__()
                            atexit.unregister(readline.write_history_file)


                            This way you'll still have access to tab-completion and previous history, but the commands you enter won't be added to the history file.






                            share|improve this answer




















                            • This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
                              – Lekensteyn
                              Feb 9 '16 at 14:39










                            • I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
                              – berdario
                              Feb 10 '16 at 0:54












                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Put the following in a file and set PYTHONSTARTUP to it (or call the file sitecustomize.py and make it accessible from the PYTHONPATH)



                            import readline
                            import atexit
                            import sys

                            sys.__interactivehook__()
                            atexit.unregister(readline.write_history_file)


                            This way you'll still have access to tab-completion and previous history, but the commands you enter won't be added to the history file.






                            share|improve this answer












                            Put the following in a file and set PYTHONSTARTUP to it (or call the file sitecustomize.py and make it accessible from the PYTHONPATH)



                            import readline
                            import atexit
                            import sys

                            sys.__interactivehook__()
                            atexit.unregister(readline.write_history_file)


                            This way you'll still have access to tab-completion and previous history, but the commands you enter won't be added to the history file.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 6 '16 at 20:44









                            berdario

                            1276




                            1276











                            • This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
                              – Lekensteyn
                              Feb 9 '16 at 14:39










                            • I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
                              – berdario
                              Feb 10 '16 at 0:54
















                            • This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
                              – Lekensteyn
                              Feb 9 '16 at 14:39










                            • I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
                              – berdario
                              Feb 10 '16 at 0:54















                            This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
                            – Lekensteyn
                            Feb 9 '16 at 14:39




                            This does not seem to work (Python 3.5.1 on Arch Linux), the history file is still written on exit even through the PYTHONSTARTUP script is indeed executed.
                            – Lekensteyn
                            Feb 9 '16 at 14:39












                            I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
                            – berdario
                            Feb 10 '16 at 0:54




                            I just tested it on my Arch Linux vm, and it works fine: gist.github.com/berdario/640b3ab00b128fdf3338
                            – berdario
                            Feb 10 '16 at 0:54










                            up vote
                            0
                            down vote



                            accepted










                            To prevent Python from writing ~/.python_history, disable the hook that activates this functionality:



                            import sys
                            # Disable history (...but also auto-completion :/ )
                            if hasattr(sys, '__interactivehook__'):
                            del sys.__interactivehook__


                            If you would like to enable tab-completion and disable the history feature, you can adapt the site.enablerlcompleter code. Write the following code to ~/.pythonrc and set export PYTHONSTARTUP=~/.pythonrc in your shell to enable it.



                            import sys
                            def register_readline_completion():
                            # rlcompleter must be loaded for Python-specific completion
                            try: import readline, rlcompleter
                            except ImportError: return
                            # Enable tab-completion
                            readline_doc = getattr(readline, '__doc__', '')
                            if readline_doc is not None and 'libedit' in readline_doc:
                            readline.parse_and_bind('bind ^I rl_complete')
                            else:
                            readline.parse_and_bind('tab: complete')
                            sys.__interactivehook__ = register_readline_completion





                            share|improve this answer
























                              up vote
                              0
                              down vote



                              accepted










                              To prevent Python from writing ~/.python_history, disable the hook that activates this functionality:



                              import sys
                              # Disable history (...but also auto-completion :/ )
                              if hasattr(sys, '__interactivehook__'):
                              del sys.__interactivehook__


                              If you would like to enable tab-completion and disable the history feature, you can adapt the site.enablerlcompleter code. Write the following code to ~/.pythonrc and set export PYTHONSTARTUP=~/.pythonrc in your shell to enable it.



                              import sys
                              def register_readline_completion():
                              # rlcompleter must be loaded for Python-specific completion
                              try: import readline, rlcompleter
                              except ImportError: return
                              # Enable tab-completion
                              readline_doc = getattr(readline, '__doc__', '')
                              if readline_doc is not None and 'libedit' in readline_doc:
                              readline.parse_and_bind('bind ^I rl_complete')
                              else:
                              readline.parse_and_bind('tab: complete')
                              sys.__interactivehook__ = register_readline_completion





                              share|improve this answer






















                                up vote
                                0
                                down vote



                                accepted







                                up vote
                                0
                                down vote



                                accepted






                                To prevent Python from writing ~/.python_history, disable the hook that activates this functionality:



                                import sys
                                # Disable history (...but also auto-completion :/ )
                                if hasattr(sys, '__interactivehook__'):
                                del sys.__interactivehook__


                                If you would like to enable tab-completion and disable the history feature, you can adapt the site.enablerlcompleter code. Write the following code to ~/.pythonrc and set export PYTHONSTARTUP=~/.pythonrc in your shell to enable it.



                                import sys
                                def register_readline_completion():
                                # rlcompleter must be loaded for Python-specific completion
                                try: import readline, rlcompleter
                                except ImportError: return
                                # Enable tab-completion
                                readline_doc = getattr(readline, '__doc__', '')
                                if readline_doc is not None and 'libedit' in readline_doc:
                                readline.parse_and_bind('bind ^I rl_complete')
                                else:
                                readline.parse_and_bind('tab: complete')
                                sys.__interactivehook__ = register_readline_completion





                                share|improve this answer












                                To prevent Python from writing ~/.python_history, disable the hook that activates this functionality:



                                import sys
                                # Disable history (...but also auto-completion :/ )
                                if hasattr(sys, '__interactivehook__'):
                                del sys.__interactivehook__


                                If you would like to enable tab-completion and disable the history feature, you can adapt the site.enablerlcompleter code. Write the following code to ~/.pythonrc and set export PYTHONSTARTUP=~/.pythonrc in your shell to enable it.



                                import sys
                                def register_readline_completion():
                                # rlcompleter must be loaded for Python-specific completion
                                try: import readline, rlcompleter
                                except ImportError: return
                                # Enable tab-completion
                                readline_doc = getattr(readline, '__doc__', '')
                                if readline_doc is not None and 'libedit' in readline_doc:
                                readline.parse_and_bind('bind ^I rl_complete')
                                else:
                                readline.parse_and_bind('tab: complete')
                                sys.__interactivehook__ = register_readline_completion






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 9 '16 at 14:59









                                Lekensteyn

                                9,580115086




                                9,580115086




















                                    up vote
                                    0
                                    down vote













                                    Another ~/.pythonrc solution:



                                    import readline
                                    readline.write_history_file = lambda *args: None





                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote













                                      Another ~/.pythonrc solution:



                                      import readline
                                      readline.write_history_file = lambda *args: None





                                      share|improve this answer






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        Another ~/.pythonrc solution:



                                        import readline
                                        readline.write_history_file = lambda *args: None





                                        share|improve this answer












                                        Another ~/.pythonrc solution:



                                        import readline
                                        readline.write_history_file = lambda *args: None






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jul 23 '16 at 17:23









                                        Waxrat

                                        1092




                                        1092




















                                            up vote
                                            0
                                            down vote













                                            Here's my method (which turned out to be basically a more reliable and pythonic version of berdario's method). It only disables writing to .python_history, but not reading from it if it exists, or appending new lines to the current instance's history. I recommend saving it as site-packages/sitecustomize.py, because site is the module that both writes to .python_history and imports sitecustomize if it exists, although naming it something else and pointing to it with PYTHONSTARTUP works too.



                                            import sys
                                            oldhook = getattr(sys, '__interactivehook__', None)
                                            if oldhook:
                                            def newhook():
                                            import readline, atexit
                                            oldhook()
                                            atexit.unregister(readline.write_history_file)
                                            sys.__interactivehook__ = newhook


                                            update: what i did for 3.7, specific to my system, not pep8:



                                            import rlcompleter,readline as r,sys
                                            def f():r.parse_and_bind('tab:complete');r.read_init_file()
                                            sys.__interactivehook__=f





                                            share|improve this answer


























                                              up vote
                                              0
                                              down vote













                                              Here's my method (which turned out to be basically a more reliable and pythonic version of berdario's method). It only disables writing to .python_history, but not reading from it if it exists, or appending new lines to the current instance's history. I recommend saving it as site-packages/sitecustomize.py, because site is the module that both writes to .python_history and imports sitecustomize if it exists, although naming it something else and pointing to it with PYTHONSTARTUP works too.



                                              import sys
                                              oldhook = getattr(sys, '__interactivehook__', None)
                                              if oldhook:
                                              def newhook():
                                              import readline, atexit
                                              oldhook()
                                              atexit.unregister(readline.write_history_file)
                                              sys.__interactivehook__ = newhook


                                              update: what i did for 3.7, specific to my system, not pep8:



                                              import rlcompleter,readline as r,sys
                                              def f():r.parse_and_bind('tab:complete');r.read_init_file()
                                              sys.__interactivehook__=f





                                              share|improve this answer
























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Here's my method (which turned out to be basically a more reliable and pythonic version of berdario's method). It only disables writing to .python_history, but not reading from it if it exists, or appending new lines to the current instance's history. I recommend saving it as site-packages/sitecustomize.py, because site is the module that both writes to .python_history and imports sitecustomize if it exists, although naming it something else and pointing to it with PYTHONSTARTUP works too.



                                                import sys
                                                oldhook = getattr(sys, '__interactivehook__', None)
                                                if oldhook:
                                                def newhook():
                                                import readline, atexit
                                                oldhook()
                                                atexit.unregister(readline.write_history_file)
                                                sys.__interactivehook__ = newhook


                                                update: what i did for 3.7, specific to my system, not pep8:



                                                import rlcompleter,readline as r,sys
                                                def f():r.parse_and_bind('tab:complete');r.read_init_file()
                                                sys.__interactivehook__=f





                                                share|improve this answer














                                                Here's my method (which turned out to be basically a more reliable and pythonic version of berdario's method). It only disables writing to .python_history, but not reading from it if it exists, or appending new lines to the current instance's history. I recommend saving it as site-packages/sitecustomize.py, because site is the module that both writes to .python_history and imports sitecustomize if it exists, although naming it something else and pointing to it with PYTHONSTARTUP works too.



                                                import sys
                                                oldhook = getattr(sys, '__interactivehook__', None)
                                                if oldhook:
                                                def newhook():
                                                import readline, atexit
                                                oldhook()
                                                atexit.unregister(readline.write_history_file)
                                                sys.__interactivehook__ = newhook


                                                update: what i did for 3.7, specific to my system, not pep8:



                                                import rlcompleter,readline as r,sys
                                                def f():r.parse_and_bind('tab:complete');r.read_init_file()
                                                sys.__interactivehook__=f






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 2 mins ago

























                                                answered Jun 1 '17 at 8:45









                                                landfill baby

                                                12




                                                12



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f121377%2fhow-can-i-disable-the-new-history-feature-in-python-3-4%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Popular posts from this blog

                                                    How to check contact read email or not when send email to Individual?

                                                    Bahrain

                                                    Postfix configuration issue with fips on centos 7; mailgun relay