22

I am launching a Python script from the command line (Bash) under Linux. I need to open Python, import a module, and then have lines of code interpreted. The console must then remain in Python (not quit it). How do I do that?

I have tried an alias like this one:

alias program="cd /home/myname/programs/; python; import module; line_of_code"

But this only starts python and the commands are not executed (no module import, no line of code treated).

What is the proper way of doing this, provided I need to keep Python open (not quit it) after the script is executed? Many thanks!

3 Answers 3

21

An easy way to do this is with the "code" module:

python -c "import code; code.interact(local=locals())"

This will drop you into an interactive shell when code.interact() is called. The local keyword argument to interact is used to prepopulate the default namespace for the interpreter that gets created; we'll use locals(), which is a builtin function that returns the local namespace as a dictionary.

Your command would look something like this:

python -c "import mymodule, code; code.interact(local=locals())"

which drops you into an interpreter that has the correct environment.

Sign up to request clarification or add additional context in comments.

5 Comments

This only works halfway. I added my import module, either before or after 'import code' but when in the interactive python console, my module is not available.
I think the code module can do what you want, but you have to bootstrap the environment like this: python -c "import sys, code; code.interact(local=locals())" This drops me to a command shell that lets me do: >>> print sys.argv[0] Is this closer to what you're trying to do?
Great! This does the job for me. My own module is now also loaded (import sys, code, mymodule;...) Cheers
Nice! What if I also want to execute some code? python -c "import mymodule, code; code.interact(local=locals()); print('ciao')" would not work..
@Alain1405 - once you call code.interact(...), you're in the interactive session. If you want to run code before you get into your interactive session, you'll have to put it before the call to code.interact(...). If you want to execute code after the interactive session ends, but before the interpreter exits, you can exit the interpreter with CTRL-d on Linux, or CTRL-z + Enter on Windows, and you should see your 'ciao' message on the terminal.
9

use a subroutine instead of alias

callmyprogram(){
  python -i -c "import time;print time.localtime()"
}
callmyprogram

3 Comments

@ghostdog74 I never use a subroutine before. Where do I put this? In the .bashrc file? (.bash_bashrc because I'm using Linux Mint) How do I then launch it? Thx.
you can put it in any script. since you have .bash_bashrc, you can put it there. if you want to use the subroutine, just do a source .bash_bashrc
+𝟣 for the combination of -i and -c, which solves the OP's problem. While putting something in a function never hurts (for added flexibility), an alias would work equally well here. A note on Bash terminology: 'subroutine' is used as an umbrella term for both shell functions and sourced scripts; therefore, it's better to call this a [shell] function.
4

Example:

python -c "import time ; print 'waiting 2 sec.'; time.sleep(2); print 'finished' "

5 Comments

@Karol Thx, but I must remain within Python in the console since this is an interactive script. I edited the question to add this.
If you need to remain 'within' python, start the interpreter with the -i flag.
I tried with the -i (with or without c-) but i get a: 'python: can't open file 'import module': [Errno 2] No such file or directory'
@Morlock: What does it mean when you say "since this is an interactive script"? Normally, you can have interactive scripts without being in the console.
@Dennis Williamson I wish to open a console, type 'myprogram', and then Python opens, imports a given module, maybe a line or 2 of script, and finally remains in the Python console and gives back the hand to the user. The user is now ready to use the program, from the console, with the '>>>' Python prompt. Cheers

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.