Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1194
  • Last Modified:

nameError

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
onyourmark
Asked:
onyourmark
  • 3
  • 2
1 Solution
 
mish33Commented:
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
 
peprCommented:
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
 
onyourmarkAuthor Commented:
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: 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.

 
peprCommented:
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
 
peprCommented:
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
 
onyourmarkAuthor Commented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now