1

Running Python 3.10. I have three files in the same directory named Chess, one of which is a __init __.py to make sure it's considered a module.

In one file, ChessMain.py I have a line: from Chess import ChessEngine

When I run the file, I get the ModuleNotFoundError: No module named 'Chess' error.

Sorry if this is a dumb import question, but I can't seem to figure out what I'm doing wrong.

3
  • 1
    can you try from .Chess import Commented Jan 17, 2022 at 5:41
  • try import ChessEngine without the Chess parent Commented Jan 17, 2022 at 5:42
  • It depends on how and where you are calling your script. Right now, Python looks for a module Chess and try to import. If you are running inside Chess , then that folder|module doesn’t exist and thus error. Either call your script outside Chess,cd .. && python Chess/ChessMain.py Or use set PYTHONPATH to tell Python where it should look Commented Jan 17, 2022 at 6:04

2 Answers 2

2

If they are in the same directory then you should be able to import a class or function from the file based off of its name:

from Chess import <class-name> 

If that doesn't work, you can try (as others have said):

from .Chess import <class-name> 

Or you can try:

from sys import path path.append(<absolute-path-to-your-directory>) from Chess import <class-name> 

The first example worked fine for me in python 3.7.3.

Sign up to request clarification or add additional context in comments.

2 Comments

It is recommended not to messy with sys.path. Use PYTHONPATH=path\to\directory python main.py
but sometimes you don't have control over environment variables.
-1

This Might help

Let's say, this is my directory content

\_ module_1.py \_ module_2.py \_ main.py 

And if I want to import it into my main.py file, I simply use

import module_1 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.