Solved

import modules globally to be used inside functions

Posted on 2011-09-12
7
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

622 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