I have just started Python recently and for some reason, I cannot import anything on my computer. I can do it on the computer at my school, but not my laptop. This is the message I received when I tried to import the program "cTurtle"

IDLE 2.6.1      
>>> import cTurtle

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import cTurtle
ImportError: No module named cTurtle
>>>

Does anyone know what I'm doing wrong?

Try something simple:

import math

print (math.sqrt(4))

If that works then is it possible you do not have the module cTurtle on your home computer?

Not all modules come with python and thus must be downloaded separately.


If that doesn't work then check to make sure whatever editor your using is pointed to the correct paths to search in for modules.

Alright, importing math worked, so that's a good sign. But I do have the cTurtle program on my computer. I have opened it from the file location and from Python to view the code, but it still will not import for me. Is it possible that it needs to be saved in a specific area to be imported?

Set pythonpath environment variable to point to location of the cTurtle.

I found the solution! Apparently, you have to be sure that the program that you want to import is within your "Lib" folder. Otherwise Python will not find the program.

I guess it was a case of looking for programs in all the wrong places. Thanks for giving me an idea of how to get it done.

# show the system path (aka. PYTHONPATH), a list of directories
# that Python by default would look into
# item at index 0 would be the working directory

import sys
print( sys.path )

"""possible output -->
[
'D:\\Python26\\Atest26\\aatest26',
'C:\\WINDOWS\\system32\\python26.zip',
'D:\\Python26\\DLLs',
'D:\\Python26\\lib',
'D:\\Python26\\lib\\plat-win',
'D:\\Python26\\lib\\lib-tk',
'D:\\Python26',
'D:\\Python26\\lib\\site-packages',
'D:\\Python26\\lib\\site-packages\\PIL',
'D:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode'
]
"""

As you can see, you can put the module cTurtle.py into any of these directories, with lib being one of the better choices. Also remember that module names are case sensitive even on a Windows system.

You dont necessarily put your cTurtle.py into any of the folders. Make sure python searches the right locations for import :

import sys
sys.path.append('/folder/in/which/cTurtle.py/resides')
import cTurtle

well the order of search goes from the current directory, to the modules within the current directory, then sys.path

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.