Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

In Python how do I correct this Module Not Found Error

I have the following module structure in my Administrator project folder

StockController.py (in the root project folder)

then in a package called Utilities I have the following two modules

  1. ExcelManipulator.py
  2. StockPurchaseInfo.py

Relevant Code from each module as as follows:

StockController.py

from Utilities.ExcelManipulator 
import ExcelManipulatorClass

ExcelManipulator.py

from StockPurchaseInfo import StockInfoClass
class ExcelManipulatorClass:

StockPurchaseInfo.py

class StockInfoClass:

When running the StockController module I get the following error

Traceback (most recent call last):
  File "d:AdministratorStockController.py", line 1, in <module>
    from Utilities.ExcelManipulator import ExcelManipulatorClass
 File "d:AdministratorUtilitiesExcelManipulator.py", line 2, in <module>
    from StockPurchaseInfo import StockInfoClass
ModuleNotFoundError: No module named 'StockPurchaseInfo'

However when I run the ExcelManipulator.py module I dont get the error (it is able to find the StockPurchaseInfo module) why is this the case?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do you have an __init__.py in your Utilities directory ?

And in your ExcelManipulator, you've to do this : from Utilities.StockPurchaseInfo import StockInfoClass (or from .StockPurchaseInfo import StockInfoClass to import relatively)

If you run your programm from the root of your project (and you should do so), modules path are defined from it (from the root of the project), so if you're doing from StockPurchaseInfo import StockInfoClass then python is looking for an StockPurchaseInfo.py in you're root project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...