Solved

import modules globally to be used inside functions

Posted on 2011-09-12
7
351 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 28

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 28

Assisted Solution

by:pepr
pepr earned 334 total points
ID: 36527763
So, the mish33 correctly said what must be done.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now