954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

from-import ImportError

Hi,

I downloaded some code from a university website that calculates a metric evolutionary trees. I'm trying to run the program (called example.py) and the first line of the program is

from TreeGenerator import Tree_generator

I did not think this would be a problem because TreeGenerator.py is another file in the folder I downloaded. However, when I try to run the program it says that it cannot find the module TreeGenerator. I tried putting the folder in my path but that didn't work. I was wondering if there was any specific place in thy python folder that I have to put modules like that. Thanks!

Elise

echellwig
Newbie Poster
21 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
 

Are these two files in exactly same directory or different subdirectories? Have you tried to put the library to site-packages directory. Is there setup.py file and have you read any included readme or instalk files?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

You can use sys.path ...

# show the system path (aka. PYTHONPATH)
# as a list of directories that Python by default looks into
# the first item will be the working directory
# the directory  C:\\Python26\\lib\\site-packages
# might be the best to add your own modules to
# even though it is meant for packages only

import sys
print(sys.path)

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


This works on Windows or Unix, only the directory names will change.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Quick Fix

from sys import path
path.append('path_to_files')
Beat_Slayer
Posting Pro in Training
405 posts since Jun 2010
Reputation Points: 30
Solved Threads: 105
 

I can't think of anything.
If it is in the same directory, then that line should work.
Check your spelling of the module name?
Sometimes it is something simple.

jcao219
Posting Pro in Training
417 posts since Dec 2009
Reputation Points: 28
Solved Threads: 97
 

do you have an __init__.py file in the module folders? Sometimes that might cause the problem

ultimatebuster
Posting Whiz in Training
250 posts since Mar 2010
Reputation Points: 24
Solved Threads: 69
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: