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.
6 1550
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?
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: - import MyModule
-
MyModule.AnyFunction(anyArgs)
is the cleanest (for the namespace of the importing module). NOTE: Leave off the .py. - from MyModule import AnyFunction
-
AnyFuncion(anyArgs)
is the one that I use most. It's very common to see things like: - from time import time
-
print time()
A third option (which is discouraged because it clutters the namespace of the importing module) is: - form MyModule import *
-
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
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
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: - import MyModule
-
MyModule.AnyFunction(anyArgs)
is the cleanest (for the namespace of the importing module). NOTE: Leave off the .py. - from MyModule import AnyFunction
-
AnyFuncion(anyArgs)
is the one that I use most. It's very common to see things like: - from time import time
-
print time()
A third option (which is discouraged because it clutters the namespace of the importing module) is: - form MyModule import *
-
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
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.
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
or
Not both.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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.
|
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'}
|
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?
|
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...
| |
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)"
|
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...
|
by: zaley |
last post by:
Is there a open souce IDE writen by C( C++) or partly writen by C( C+
+)?
|
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...
|
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...
|
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...
| |
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |