Solved

nameError

Posted on 2007-11-20
6
1,163 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
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

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

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 28

Accepted Solution

by:
pepr earned 125 total points
Comment Utility
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 28

Expert Comment

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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 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…

771 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