Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

import modules globally to be used inside functions

Posted on 2011-09-12
7
Medium Priority
?
357 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 664 total points
ID: 36527615
Import random on top of createunif.py before def createunif.
0
 
LVL 29

Accepted Solution

by:
pepr earned 1336 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 1336 total points
ID: 36527763
So, the mish33 correctly said what must be done.
0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The viewer will learn how to dynamically set the form action using jQuery.
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.

722 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