473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling previously writen python scripts [How to use import]

54 New Member
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 1550
ilikepython
844 Recognized Expert Contributor
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 Recognized Expert Expert
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
Patrick C
54 New Member
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
Patrick C
54 New Member
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 Recognized Expert Contributor
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()".Exc ept 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 Recognized Expert Expert
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

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

Similar topics

13
4012
by: Peter Mutsaers | last post by:
Hello, Up to now I mostly wrote simple filter scripts in Perl, e.g. while(<>) { # do something with $_, regexp matching, replacements etc. print; } Now I learned Python and like it much more as a language.
4
3830
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
7
1353
by: Doug Rosser | last post by:
I'm writing a fairly complicated test framework and keeping configuration data inside ini files that are parsed at runtime by the ConfigParser module. For example, there would be a section similar to the following server1:{'hostname':'alpha','os':'posix'} server2:{'hostname':'beta','os':'win'}
22
6291
by: Brad Tilley | last post by:
Is it possible to write a file open, then read program in C and then call the C program from a Python script like this: for root, files, dirs in os.walk(path) for f in files: try: EXECUTE_C_PROGRAM If possible, how much faster would this be over a pure Python solution?
5
2147
by: Pekka Niiranen | last post by:
Hi there, I have two scripts. The first "main.py" sets some variables and then imports another called "gen.py". The idea is to provide "main.py" that defines some paths, variables etc. without using Windows environment variables. Various other "hackers" will make additional Python scripts (subroutines) like "gen.py" that utilize variables...
3
2604
by: krzychu | last post by:
Hi, I have installed brand new platform - Zope-2-7-6, Python 2.4.1, Plone 2.0.5, OS Debian 1:3.3.6-2. After import a old Plone site from the following platform Zope-2-7-4, Python 2.3.3, Plone 2.0.3 to the new one, I get error when I visit PuthonScript in the ZMI. "invalid syntax (Script (Python), line 1)"
10
9901
by: TokiDoki | last post by:
Hello there, I have been programming python for a little while, now. But as I am beginning to do more complex stuff, I am running into small organization problems. It is possible that what I want to obtain is not possible, but I would like the advice of more experienced python programmers. I am writing a relatively complex program in...
16
1161
by: zaley | last post by:
Is there a open souce IDE writen by C( C++) or partly writen by C( C+ +)?
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.