Solved

nameError

Posted on 2007-11-20
6
1,169 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
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 28

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
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.

 
LVL 28

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 28

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

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

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…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

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