469,293 Members | 1,319 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

calling previously writen python scripts [How to use import]

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
6 1382
ilikepython
844 Expert 512MB
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
6,596 Expert 4TB
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
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
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
844 Expert 512MB
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
6,596 Expert 4TB
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.

Similar topics

13 posts views Thread by Peter Mutsaers | last post: by
4 posts views Thread by Logan | last post: by
22 posts views Thread by Brad Tilley | last post: by
3 posts views Thread by krzychu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.