Solved

nameError

Posted on 2007-11-20
6
1,183 Views
Last Modified: 2010-04-21
Hi. I am trying to learn to use Python. I just ran a command from the command line
From recommendations import critics

That worked.
Then a line or two later I wrote:
reload(recommendations)

but I get NameError name recommendations is not defined.

Can you explain this?
Sorry for stupid beginner question
0
Comment
Question by:onyourmark
[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
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:mish33
ID: 20326248
When you use 'from package import module' only module get into your namespace, so package remains unknown hence NameError.
Use reload(module).
If you're importing a function/class, just del it and reimport.
0
 
LVL 29

Expert Comment

by:pepr
ID: 20326878
mish33 is right about why you cannot see the module name. However, I am not sure what mish33 meant by the last word "reimport"...

Try to simulate the problem with the following two simple scripts located in the same directory:

recommendations.py
===================================
critics = ['a', 'b', 'c', 'd']
===================================

You will modify it using a text editor when running one of the following test scripts. They run in infinite loop (stop them by Ctrl-C), do the actions related to the reloading and print the current content of the critics.

The easiest way is to use the simple form of import statement and fully qualified names:

test1.py
===================================
import recommendations
import time

while True:
    reload(recommendations)
    print recommendations.critics
    time.sleep(1)
===================================

If the only reason for "from recommendations import critics" is to obtain the short name "critics" instead of fully qualified but long "recommendations.critics", you can simply create new name for the long name:

test2.py
===================================
import recommendations
import time

while True:
    reload(recommendations)
    critics = recommendations.critics
    print critics
    time.sleep(1)
===================================

The only difference is that the module name is visible.
0
 

Author Comment

by:onyourmark
ID: 20332713
Hi, can you tell me what "so you should save
recommendations.py somewhere where the Python interactive interpreter can find it.
This could be in the python/Lib directory, but the easiest way to do it is to start the
Python interpreter in the same directory in which you saved the file." means? I mean how do I start the interpreter in the same directory?
Thanks!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 29

Accepted Solution

by:
pepr earned 125 total points
ID: 20333027
I do not know where the citation "so you should save recommendations.py somewhere where..." came from. I would recommend to read the official Tutorial, chapter "6. Modules" (http://docs.python.org/tut/node8.html). Pay attention to subsection "6.1.1 The Module Search Path" (http://docs.python.org/tut/node8.html#SECTION008110000000000000000) that explains where the modules are searched for.
0
 
LVL 29

Expert Comment

by:pepr
ID: 20333047
When starting a script, it is usual to start it or explicitly by passing it to the python interpreter as command line argument or implicitly via association for the extension (Windows) or via marking it as executable and using the magic sequence at the first line (i.e. name of the interpreter; in Unix-like systems).

If you use explicit path or start it from the working directory, then the path need not to be named in the Python search path list. For your own module, it is usual to put them to the same directory as the script with the body of the program. If recommendations.py is your module, then put it to the same directory as for example test1.py.
0
 

Author Closing Comment

by:onyourmark
ID: 31410312
Thanks very much. So if I want to run a file from a different directory I assume that I should add that (with semicolon) to the environment variable PYTHONPATH.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…
Suggested Courses

632 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