run python command line interpreter with imports loaded automatically

后端 未结 5 565
执笔经年
执笔经年 2021-02-07 02:54

I would like to play around in the python interpreter but with a bunch of imports and object setup completed. Right now I\'m launching the interpreter on the command line and do

相关标签:
5条回答
  • 2021-02-07 03:08

    You can use the -s option while starting the command line. The details are given in the documentation here

    0 讨论(0)
  • 2021-02-07 03:09

    I came across this question when trying to configure a new desk for my research and found that the answers above didn't quite suit my desire: to contain the entire desk configuration within one file (meaning I wouldn't create a separate script.py as suggested by @srgerg).

    This is how I ended up achieving my goal:

    export PYTHONPATH=$READ_GEN_PATH:$PYTHONPATH
    
    alias prepy="python3 -i -c \"
    from naive_short_read_gen import ReadGen
    from neblue import neblue\""
    

    In this case neblue is in the CWD (so no path extension is required there), whereas naive_short_read_gen is in an arbitrary directory on my system, which is specified via $READ_GEN_PATH.

    You could do this in a single line if necessary: alias prepy=PYTHONPATH=$EXTRA_PATH:$PYTHONPATH python3 -i -c ....

    0 讨论(0)
  • 2021-02-07 03:13

    I use PYTHONSTARTUP.

    My .bash_profile has a path to my home folder .pyrc, which as the import statements in it.

    https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP

    0 讨论(0)
  • 2021-02-07 03:23

    You can create a script with the code you wish to run automatically, then use python -i to run it. For example, create a script (let's call it script.py) with this:

    import foo
    import baz
    l = [1,2,3,4]
    

    Then run the script

    $ python -i script.py
    >>> print l
    [1, 2, 3, 4]
    

    After the script has completed running, python leaves you in an interactive session with the results of the script still around.

    If you really want some things done every time you run python, you can set the environment variable PYTHONSTARTUP to a script which will be run every time you start python. See the documentation on the interactive startup file.

    0 讨论(0)
  • 2021-02-07 03:26

    I think I know what you want to do. You might want to check IPython, because you cannot start the python interpreter without giving the -i option (at least not directly). This is what I did in my project:

    def ipShell():
        '''Starts the interactive IPython shell'''
        import IPython
        from IPython.config.loader import Config
        cfg = Config()
        cfg.TerminalInteractiveShell.confirm_exit = False
        IPython.embed(config=cfg, display_banner=False)
    # Then add the following line to start the shell
    ipShell()
    

    You need to be careful, though, because the shell will have the namespace of the module that the function ipShell() is defined. If you put the definition in the file you run, then you will be able to access the globals() you want. There could be other workarounds to inject the namespace you want, b̶u̶t̶ ̶y̶o̶u̶ ̶w̶o̶u̶l̶d̶ ̶h̶a̶v̶e̶ ̶t̶o̶ ̶g̶i̶v̶e̶ ̶a̶r̶g̶u̶m̶e̶n̶t̶s̶ ̶t̶o̶ ̶t̶h̶e̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶i̶n̶ ̶t̶h̶a̶t̶ ̶c̶a̶s̶e̶.

    EDIT

    The following function defaults to caller's namespace (__main__.__dict__).

    def ipShell():
        '''Starts the interactive IPython shell
        with the namespace __main__.__dict__'''
        import IPython
        from __main__ import __dict__ as ns
        from IPython.config.loader import Config
        cfg = Config()
        cfg.TerminalInteractiveShell.confirm_exit = False
        IPython.embed(config=cfg, user_ns=ns, display_banner=False)
    

    without any extra arguments.

    0 讨论(0)
提交回复
热议问题