The Famous Error Message: "ImportError: No module named python_script"

R

rich murphy

I am studying Python language. I have Python 2.5 installed in my PC
which is running on Windows XP. I placed the the script called
"python_script" in C:\Python25 directory where all the other Python
files are.

When I tried to import the script called "python_script", it kept
printing the famous error message: "ImportError: No module named
python_script".

I took the file out of "C:\Python25" directory, placed it in
'C:\PythonTests'. The error message kept coming.

The "import" commnand will not work at all.


Traceback (most recent call last):
['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs',
'C:\\Python25\
\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk',
'C:\\Python25
', 'C:\\Python25\\lib\\site-packages']
['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs',
'C:\\Python25\
\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk',
'C:\\Python25
', 'C:\\Python25\\lib\\site-packages']
['', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs',
'C:\\Python25\
\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk',
'C:\\Python25
', 'C:\\Python25\\lib\\site-packages', 'C:\\PythonTests']


I uninstall Python2.5, installed Python2.4: the result is the same. The
result is the same with older versions of Python also.

Does anybody know a remedy for this???
 
G

Gabriel Genellina

I am studying Python language. I have Python 2.5 installed in my PC
which is running on Windows XP. I placed the the script called
"python_script" in C:\Python25 directory where all the other Python
files are.

Verify the file name, should be "python_script.py"

You may want to un-select "Hide extensions for known file types" in
Windows Explorer options.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
B

Ben Finney

rich murphy said:
I am studying Python language.

Welcome! Allow me to direct you to the Python tutorial:

<URL:http://docs.python.org/tut/>

Please take the time to work through all the exercises in that
document, understanding each one before moving on.

I recommend this because:
I placed the the script called "python_script" in C:\Python25
directory where all the other Python files are.

you would not make this mistake if you had already worked through the
tutorial.

Enjoy!
 
R

rich murphy

Thank you both for responding.

Yes of course the file has the ".py" extension and yes I went through
the tutorial.
 
F

Fredrik Lundh

rich said:
Thank you both for responding.

Yes of course the file has the ".py" extension and yes I went through
the tutorial.

since everyone on this forum is importing modules successfully hundreds
of times every day, that's not obvious at all.

try running the interpreter as

python -vv

and see what it prints when you type

import python_script

</F>
 
R

rich murphy

Ben said:
Welcome! Allow me to direct you to the Python tutorial:

<URL:http://docs.python.org/tut/>

Please take the time to work through all the exercises in that
document, understanding each one before moving on.

I recommend this because:


you would not make this mistake if you had already worked through the
tutorial.

The tutorial says: "For instance, use your favorite text editor to
create a file called fibo.py in the current directory with the
following contents:"

So, I assumed "the current directory" is C:\Python25 which did not
work. Then I placed the fibo.py file in C: director. That did not work
either. What directory does it mean then?
 
E

Eric Pederson

rich said:
So, I assumed "the current directory" is C:\Python25 which did not
work. Then I placed the fibo.py file in C: director. That did not work
either. What directory does it mean then?
OK, forgive me for using 2.4... Can you import "sys"? Assuming you've
got python_script.py at path: "C:\\python_script.py" you might try this
quick test:
['C:\\Python24\\Lib\\idlelib', 'C:\\windows\\system32\\python24.zip',
'C:\\Python24', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib',
'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk',
'C:\\Python24\\lib\\site-packages',
'C:\\Python24\\lib\\site-packages\\win32',
'C:\\Python24\\lib\\site-packages\\win32\\lib'] ## your ouput may be
different['C:\\Python24\\Lib\\idlelib', 'C:\\windows\\system32\\python24.zip',
'C:\\Python24', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib',
'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk',
'C:\\Python24\\lib\\site-packages',
'C:\\Python24\\lib\\site-packages\\win32',
'C:\\Python24\\lib\\site-packages\\win32\\lib', 'C:\\']

6.1.1 The Module Search Path

When a module named spam is imported, the interpreter searches for a
file named spam.py in the current directory [...]

Actually, modules are searched in the list of directories given by the
variable |sys.path| which is initialized from the directory containing
the input script (or the current directory), PYTHONPATH and the
installation-dependent default. This allows Python programs that know
what they're doing to modify or replace the module search path. Note
that because the directory containing the script being run is on the
search path, it is important that the script not have the same name as a
standard module, or Python will attempt to load the script as a module
when that module is imported. This will generally be an error. See
section 6.2 <#standardModules>, ``Standard Modules,'' for more information.
 
G

Gabriel Genellina

The tutorial says: "For instance, use your favorite text editor to
create a file called fibo.py in the current directory with the
following contents:"

So, I assumed "the current directory" is C:\Python25 which did not
work. Then I placed the fibo.py file in C: director. That did not work
either. What directory does it mean then?

It doesn't matter *which* directory you are, as far as you don't
change it when you enter the interpreter:

C:\TEMP\test\basura>type fibo.py
print "inside",__file__

C:\TEMP\test\basura>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
When it later says "enter the Python interpreter" that means "execute
python from the command line", not PythonWin nor IDLE nor...


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
R

rich murphy

Thanks to everyone who responded with valuable suggestions. I
appreciate them all. I found the exact reason why "import" command
fails.

At the beginning I created my script file with MS Notepad. After
studying all the advice, I realized that I did not try other text
editors just becuase the tutorial says: "use your favorite text editor
to create a file called fibo.py in the current directory with the
following contents:"

Then I created another file with the same stuff in it using WordPad.
The behaviour of the "import" command improved a lot but still not
good. Then I used MS-DOS' text editor to create the same file.
Immediately the "import" command worked as it was supposed to. Almost.
"fibo.fib2(100)" command stil does not work.


This is fine:1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

This not fine:[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]


When I enter "fibo.fib2(100)", I get the following:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\fibo.py", line 11, in fib2
while b < n:
UnboundLocalError: local variable 'b' referenced before assignment


This is fine:1 1 2 3 5 8 13 21 34 55 89 144 233 377

Both Python2.4 and 2.5 are behaving the same. I will also try to create
the file with Vi editor.
 
R

Roel Schroeven

rich murphy schreef:
This not fine:[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]


When I enter "fibo.fib2(100)", I get the following:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\fibo.py", line 11, in fib2
while b < n:
UnboundLocalError: local variable 'b' referenced before assignment

Can you show the code of fib2? It looks like there's an error in it, but
without the code it's hard to say anything about it.
Both Python2.4 and 2.5 are behaving the same. I will also try to create
the file with Vi editor.

If by Vi you mean Vim, that's my favorite editor for editing Python
code, with Idle second.
 
G

Gabriel Genellina

Thanks to everyone who responded with valuable suggestions. I
appreciate them all. I found the exact reason why "import" command
fails.

At the beginning I created my script file with MS Notepad. After
studying all the advice, I realized that I did not try other text
editors just becuase the tutorial says: "use your favorite text editor
to create a file called fibo.py in the current directory with the
following contents:"

I bet your file is actually called fibo.py.txt then.
(Perhaps some advise should be provided for Windows users, about
enclosing the name inside "double quotes", or selecting "All files
(*.*)" on the file type list)
Then I created another file with the same stuff in it using WordPad.
The behaviour of the "import" command improved a lot but still not
good.

Uhm... either the import was successful, or not. Care to explain what
you mean?
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Python24\fibo.py", line 11, in fib2
while b < n:
UnboundLocalError: local variable 'b' referenced before assignment

Compare your function with the version shown in the tutorial.
Variable 'b' is assigned on the previous line.


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
G

greg

rich said:
So, I assumed "the current directory" is C:\Python25 which did not
work... What directory does it mean then?

It means the current directory of the process running
the Python interpreter, which, unless you've done
something to change it, will be the same as the
current directory of the command shell that you
ran the Python interpreter from. That, in turn,
will be whatever you set it to using the "cd" command.

However, it's important to understand that this only
applies when using the Python interpreter *interactively*,
which means just typing "python" and entering Python
statements at the >>> prompt.

If you tell the interpreter to run a .py file,
e.g.

python some\dir\myfile.py

then it does *not* look in the current directory of
the process for imported files -- instead, it looks in the
directory containing the "main" .py file, i.e. the one
you passed on the command line. This is more useful,
since it allows you to keep the main file and the
modules it uses together, and not have to worry about
cd'ing to the directory in order to run it.

Also, if you're using an IDE such as IDLE or PythonWin
instead of running the interpreter from a command shell,
relying on the "current directory" isn't very useful,
since it's hard to predict what it will be. Again,
just put the files to be imported alongside your main
..py file and you should be all right.

There are various ways of specifying additional places
to look for imported modules, but that should be enough
to get you going.
 
G

greg

Gabriel said:
I bet your file is actually called fibo.py.txt then.
(Perhaps some advise should be provided for Windows users, about
enclosing the name inside "double quotes", or selecting "All files
(*.*)" on the file type list)

Also, if by some chance you've got "Hide filename
extensions" turned on in your View options, TURN
IT OFF. It'll cause no end of confusion when you're
trying to program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top