By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,551 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,551 IT Pros & Developers. It's quick & easy.

calling previously writen python scripts [How to use import]

P: 54
Python world,

I've writen a simple script and saved in text editor as a .py file. I'd like to now call that script within a new scrip i've written (so that I dont ahve to retype the whole content of the previously saved .py file)

What's the command for that?

Thanks

Pat, the simpleton.
May 3 '07 #1
Share this Question
Share on Google+
6 Replies


ilikepython
Expert 100+
P: 844
Python world,

I've writen a simple script and saved in text editor as a .py file. I'd like to now call that script within a new scrip i've written (so that I dont ahve to retype the whole content of the previously saved .py file)

What's the command for that?

Thanks

Pat, the simpleton.
You can use import. Import runs the selected module and all names from that module are accessable. Would that work for you?
May 3 '07 #2

bartonc
Expert 5K+
P: 6,596
Python world,

I've writen a simple script and saved in text editor as a .py file. I'd like to now call that script within a new scrip i've written (so that I dont ahve to retype the whole content of the previously saved .py file)

What's the command for that?

Thanks

Pat, the simpleton.
As my friend, ilikepython, says: import is the key:
Expand|Select|Wrap|Line Numbers
  1. import MyModule
  2. MyModule.AnyFunction(anyArgs)
is the cleanest (for the namespace of the importing module). NOTE: Leave off the .py.
Expand|Select|Wrap|Line Numbers
  1. from MyModule import AnyFunction
  2. AnyFuncion(anyArgs)
is the one that I use most. It's very common to see things like:
Expand|Select|Wrap|Line Numbers
  1. from time import time
  2. print time()
A third option (which is discouraged because it clutters the namespace of the importing module) is:
Expand|Select|Wrap|Line Numbers
  1. form MyModule import *
  2. AnyFunction() #in MyModule
Note that import actually runs all the commands in the module being imported. So, if it's written as a "script" (in-line statements without function defs) you don't need to call any functions to make it run. Simply
Expand|Select|Wrap|Line Numbers
  1. import MyModule
May 3 '07 #3

P: 54
You can use import. Import runs the selected module and all names from that module are accessable. Would that work for you?
Sorry I should clarify,

I have that much, but in the script i wrote, has functions I want to access. How would I access those functions, or should they already be there?

For example my script ends with a print statment. Now I don't want to do all th work of writing the script so I just say

Import previousScripty
print DefinedFunction

(or maybe i'm doing that part wrong, would i need import previousScript.py ?)

thanks
May 3 '07 #4

P: 54
Thanks again guys

So if i'm clear here...
lets say I have a file named AAA.py
in AAA i have a a defined function that goes
def BBB():
blah blah blah

So now in a new script i can say...

Import AAA
From AAA import BBB ?

Thanks
PC

As my friend, ilikepython, says: import is the key:
Expand|Select|Wrap|Line Numbers
  1. import MyModule
  2. MyModule.AnyFunction(anyArgs)
is the cleanest (for the namespace of the importing module). NOTE: Leave off the .py.
Expand|Select|Wrap|Line Numbers
  1. from MyModule import AnyFunction
  2. AnyFuncion(anyArgs)
is the one that I use most. It's very common to see things like:
Expand|Select|Wrap|Line Numbers
  1. from time import time
  2. print time()
A third option (which is discouraged because it clutters the namespace of the importing module) is:
Expand|Select|Wrap|Line Numbers
  1. form MyModule import *
  2. AnyFunction() #in MyModule
Note that import actually runs all the commands in the module being imported. So, if it's written as a "script" (in-line statements without function defs) you don't need to call any functions to make it run. Simply
Expand|Select|Wrap|Line Numbers
  1. import MyModule
May 3 '07 #5

ilikepython
Expert 100+
P: 844
Thanks again guys

So if i'm clear here...
lets say I have a file named AAA.py
in AAA i have a a defined function that goes
def BBB():
blah blah blah

So now in a new script i can say...

Import AAA
From AAA import BBB ?

Thanks
PC
Yes, that is exactly the way. Then you would just call the function like "BBB()".Except remember that you don't need the first import statement if you are only using that one function.
May 3 '07 #6

bartonc
Expert 5K+
P: 6,596
Thanks again guys

So if i'm clear here...
lets say I have a file named AAA.py
in AAA i have a a defined function that goes
def BBB():
blah blah blah

So now in a new script i can say...

Import AAA
From AAA import BBB ?

Thanks
PC
As my friend says: Use either
Expand|Select|Wrap|Line Numbers
  1. Import AAA
or
Expand|Select|Wrap|Line Numbers
  1. From AAA import BBB
Not both.
May 3 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.