Hi,

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

from TreeGenerator import Tree_generator

I did not think this would be a problem because 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

Dani AI

Generated

A few common causes explain why a local file named TreeGenerator.py might still trigger ImportError even when it looks like both files are “in the same folder.” The usual culprits are: the script being run from a different working directory, case/extension mismatches (e.g. TreeGenerator.py vs treegenerator.py or TreeGenerator.py.txt), a package vs module layout that requires package-context imports, or a namespace collision with an installed module of the same name. The suggestions already posted point in the right directions; this note ties them together and gives concrete, robust fixes.

Checklist (what to verify and why)

  • Confirm the actual filename and extension exactly match the import (case matters on Linux/macOS).
  • Make sure the process is started with the folder containing both files as the import-search context (running a script from another directory can change what Python sees).
  • If TreeGenerator is inside a package folder (it may be a directory or the project is laid out as a package), ensure there is an init.py where needed and run the module in package context rather than as a loose script. Running as a package avoids relative-import surprises.
  • Watch for name collisions: an installed package or a different file earlier on sys.path can shadow the local file.
  • If the project ships an installer (setup.py / pyproject), prefer installing it (editable install while developing) rather than monkey-patching sys.path.

Two practical fixes (examples)

  • Run inside the package context (replace packagename with the real package folder):
    python -m packagename.example
  • Install the project for development (if it has setup.py or a pyproject):
    pip install -e .

Notes on earlier replies: is right to inspect Python’s import search path; ’s sys.path append is a handy quick test but brittle long‑term; ’s init.py point is important for package layouts; and highlighted setup/spelling issues worth checking. If those checks are followed, the cause is almost always one of the items above and can be resolved without moving files into global site-packages.

Recommended Answers

All 5 Replies

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

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.

Quick Fix

from sys import path
path.append('path_to_files')

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.

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

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.