Solved

import modules globally to be used inside functions

Posted on 2011-09-12
7
353 Views
Last Modified: 2012-05-12
Hi,

When I program in python I normally define a main.py script, and functions/classes .py in a separate script.

I.e.,

main.py would contain:
# main
import random
from createunif import *
s = createunif(2,9)
print "mi numero es:"+string(s)

and
createunif.py would contain:
def createunif(a,b):
      return random.uniform(a,b)

However, random has not been imported to createunif so I get an error!  how can i make such that all the modules I import in main can be used by any function i call outside of the main.py script???

The error I keep getting is:

MacBook-Pro:Desktop uname$ python main.py
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    s = createunif(2,9)
  File "/Users/uname/Desktop/createunif.py", line 2, in createunif
    return random.uniform(a,b)
NameError: global name 'random' is not defined

I don't want to have to use import random inside a function, does not look very good, neither organized to me...

Any suggestions?

Thanks!
0
Comment
Question by:dfernan
  • 4
  • 2
7 Comments
 
LVL 15

Assisted Solution

by:mish33
mish33 earned 166 total points
ID: 36527615
Import random on top of createunif.py before def createunif.
0
 
LVL 29

Accepted Solution

by:
pepr earned 334 total points
ID: 36527762
I am afraid it cannot be done in a normal way.  Actually, there is nothing like global variables in Python.  The keyword "global" is rather misleading.  The "global" always mean only "global inside this module".

The module is represented by the system object. Its "global" level will always be at the module's level, not outside the module.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 334 total points
ID: 36527763
So, the mish33 correctly said what must be done.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:dfernan
ID: 36538071
Well, not what I imagine but that's just how python is, thanks!
0
 
LVL 29

Expert Comment

by:pepr
ID: 36538269
But you can still ask!  You can describe better what you want to know.  There is probably still some misunderstanding, because Python does it in a very natural way.  What is your expectation?  Is your mental picture somehow influenced by your experience with some other programming language?
0
 

Author Comment

by:dfernan
ID: 36538309
Hi mmmm it's just that I'd like to be able to define my modules in the main function and then make them usable by all the other functions... seems weird to me that that's not possible... you are right, i am probably very bias towards my previous programming experience...

Thanks a lot!
0
 
LVL 29

Expert Comment

by:pepr
ID: 36538536
Any module acts as an island in the sea.  It has its own name, its own namespace (of the same name as the module has), and its own space for objects (its own globals).  Whenever you need something from another island on your own island, you have to import it.

Part of the problem could be that you may think wrongly about the modules.  It is apparent from the name that you gave to your createunif.py.  It is the name for the action; however, it should be name for the subject (the module, the island).  The name of the function inside the module is fine, because calling a function is an action.  It should be verb.

Anything that does not act, should be given a noun identifier (a module, a class, a variable).  Anything that causes action should be given a verb identifier (a function, a method [of the class]).  Try to get used to that.  It is important when making clear mental pictures in your mind when you are solving a problem.

You should always think about a module as about a unit that could be used from various other modules.  If the module implements something, it should be as independent on the others as possible (think about the life on an island).  If it need "random", then it must be imported inside the module.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question