-2

I have a very big project going on with python. And i need to get script what opens another .py file. I have tried to use script like this:

os.system("example.py arg")

And it if the script in file is small like function print or something. But if they are long or something they don't work ! So i need a script that really works and opens the .py file in command promt. Thank you very much !

user1994934
  • 4,323
  • 2
  • 14
  • 8
  • 1
    Is should always work. Probably the problem is in your example.py file, but I can't help you without more information about the issue. – iurisilvio Jan 21 '13 at 14:38
  • Why not use `import`? If you're trying to use a function from another file, `import` will work. – Rushy Panchal Jan 21 '13 at 14:47
  • 1
    @iurisilvio I couldn't put more information about the issue because i had formatting problems when adding script in comment. And the problem in example.py was that i didn't have shebang. Thanks to Rashan Gandi , i now have. – user1994934 Jan 21 '13 at 15:40

2 Answers2

1

Running a script using

os.system("example.py arg")

requires

  • the script must be executable (+x bit)
  • the must contain the shebang "#!/usr/bin/python" at the beginning of the script

See also Should I put #! (shebang) in Python scripts, and what form should it take?

Community
  • 1
  • 1
0

If you are trying to get a function from another file, you can use import.

from example import my_funct # not that 'example' does NOT have '.py' following it
my_funct() # you can now use the function from the file.

Alternatively, you can just import the whole script:

import example
example.my_funct() # you have to specify that 'my_funct' is from 'example'

Or, you can do:

from example import * # this imports all functions from 'example'
my_funct() # now you can use the function as you normally would

NOTE: To import a function from a module, you need to have it set up correctly. This is how you add a function to your PYTHONPATH.

Community
  • 1
  • 1
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94