How to execute python program in python shell in Linux importing a special environment

Tazkera Haque Trina

I use a piece of astrophysical software called AMUSE, which uses python command line. I have got the binary release that imports amuse in terminal. Now if I want to run a saved python program in any directory, how do I call it?

Previously I used in terminal

python first.py 
pwd=secret;database=master;uid=sa;server=mpilgrim

The first.py looks like this

def buildConnectionString(params):
    """Build a connection string from a dictionary of parameters.

    Returns string."""
    return ";".join(["%s=%s" % (k, v) for k, v in params.items()])

if __name__ == "__main__":
    myParams = {"server":"mpilgrim", \
                "database":"master", \
                "uid":"sa", \
                "pwd":"secret" \
                }
    print buildConnectionString(myParams)

And my codes worked, now I am in python shell

 Python 2.7.2 (default, Dec 19 2012, 16:09:14)  [GCC 4.4.6] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import amuse
 >>>

So if I want the output of any code here, how do I proceed?

I had a program saved in my Pictures/practicepython directory, how can I call that particular .py files in python shell?

with import command, I am getting this error msg

Python 2.7.2 (default, Dec 19 2012, 16:09:14) 
[GCC 4.4.6] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import amuse
>>> import first
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named first
>>> 
Kevin

If a Python module is designed properly, it will have a few lines like this, usually near the end of the module:

if __name__ == '__main__':
    main()  # or some other code here

Assuming first.py looks like that, you can just call the main() function:

>>> import first
>>> first.main()

Note that main() might raise SystemExit, which will cause the REPL to exit. If this matters to you, you can catch it with a try block:

>>> import first
>>> try:
...     first.main()
... except SystemExit:
...     pass

Unfortunately, some modules don't have a proper main() function (or anything similar), and simply put all their top-level code in the if. In that case, there's no straightforward way to run the module from the REPL, short of copying the code.

If a Python module is not designed properly at all, it will run as soon as you import it. This is usually considered a Bad Thing because it makes it harder for others to use the module programmatically (e.g. calling the module's functions, instantiating its classes, etc.).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute python program in python shell in Linux importing a special environment

From Dev

how to execute a python program in a shell script

From Dev

How to execute python program using a shell script (and makefile?)

From Dev

Execute a shell script with ampersand from a python program

From Java

importing pyspark in python shell

From Dev

How to execute python script in Linux?

From Dev

Is it possible to execute a Python program between backticks in a shell command?

From Dev

Is it possible to execute a Python program between backticks in a shell command?

From Dev

syntax error while trying to execute python program in bash shell

From Dev

Shell Script: execute & repeat a python program that needs extra input

From Dev

Importing creating an environment variable in Python?

From Dev

python importing file on virtual environment

From Dev

Importing creating an environment variable in Python?

From Dev

python importing file on virtual environment

From Dev

How does a shell execute a program?

From Dev

How to Parallelize a Python program on Linux

From Dev

how to execute the date command in a python program

From Dev

How to have python program listen on an environment variable?

From Dev

How to have python program listen on an environment variable?

From Java

How to execute a Python script from the Django shell?

From Dev

how to execute shell script in the same process in python

From Dev

How to activate python virtual environment by shell script

From Dev

How to set environment variables of parent shell in Python?

From Dev

How to activate python virtual environment by shell script

From Dev

Execute shell commands in Python

From Dev

How to use python to start an interactive shell program?

From Dev

How to clear the shell while the program is running in Python?

From Dev

Execute Linux file written in Python using Java program

From Dev

How to open adb shell and execute commands inside shell using python

Related Related

  1. 1

    How to execute python program in python shell in Linux importing a special environment

  2. 2

    how to execute a python program in a shell script

  3. 3

    How to execute python program using a shell script (and makefile?)

  4. 4

    Execute a shell script with ampersand from a python program

  5. 5

    importing pyspark in python shell

  6. 6

    How to execute python script in Linux?

  7. 7

    Is it possible to execute a Python program between backticks in a shell command?

  8. 8

    Is it possible to execute a Python program between backticks in a shell command?

  9. 9

    syntax error while trying to execute python program in bash shell

  10. 10

    Shell Script: execute & repeat a python program that needs extra input

  11. 11

    Importing creating an environment variable in Python?

  12. 12

    python importing file on virtual environment

  13. 13

    Importing creating an environment variable in Python?

  14. 14

    python importing file on virtual environment

  15. 15

    How does a shell execute a program?

  16. 16

    How to Parallelize a Python program on Linux

  17. 17

    how to execute the date command in a python program

  18. 18

    How to have python program listen on an environment variable?

  19. 19

    How to have python program listen on an environment variable?

  20. 20

    How to execute a Python script from the Django shell?

  21. 21

    how to execute shell script in the same process in python

  22. 22

    How to activate python virtual environment by shell script

  23. 23

    How to set environment variables of parent shell in Python?

  24. 24

    How to activate python virtual environment by shell script

  25. 25

    Execute shell commands in Python

  26. 26

    How to use python to start an interactive shell program?

  27. 27

    How to clear the shell while the program is running in Python?

  28. 28

    Execute Linux file written in Python using Java program

  29. 29

    How to open adb shell and execute commands inside shell using python

HotTag

Archive