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

x
?
Solved

nameError

Posted on 2007-11-20
6
Medium Priority
?
1,190 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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
LVL 29

Accepted Solution

by:
pepr earned 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

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…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

730 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