views:

86

answers:

3

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!

+2  A: 

Example:

python -c "import time ; print 'waiting 2 sec.'; time.sleep(2); print 'finished' "
Karol
@Karol Thx, but I must remain within Python in the console since this is an interactive script. I edited the question to add this.
Morlock
If you need to remain 'within' python, start the interpreter with the `-i` flag.
ChristopheD
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
@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
@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
Morlock
+1  A: 

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.

Alex Jordan
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.
Morlock
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?
Alex Jordan
@Alex Jordan Great! This does the job for me. My own module is now also loaded (import sys, code, mymodule;...) Cheers
Morlock
+2  A: 

use a sub routine instead of alias

callmyprogram(){
  python -i -c "import time;print time.localtime()"
}
callmyprogram
ghostdog74
@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.
Morlock
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`
ghostdog74