Solved

import modules globally to be used inside functions

Posted on 2011-09-12
7
350 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
Comment Utility
Import random on top of createunif.py before def createunif.
0
 
LVL 28

Accepted Solution

by:
pepr earned 334 total points
Comment Utility
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
Comment Utility
So, the mish33 correctly said what must be done.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.

762 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

11 Experts available now in Live!

Get 1:1 Help Now