开发者

Why Python cannot find the module at the current directory?

I'm using Python 3.11.1 embeddable package. Let's say I have the following directory structure:

src  
  |---  test.py
  |---  mytest.py
test.py:
from mytest import *

After cd-ing to directory src, I run python.exe test.py, it showed me the module error:

ModuleNotFoundError: No module named 'mytest'

Changing the content of test.py to import mytest does not work. 开发者_Python百科Adding an empty __init__.py does not work as well. Switching to another Python version does not work.

Note: It's fine if I use another Python which is installed by an installer.


ModuleNotFoundError, because by default Python interpreter will check for the file in the current directory only, and need to set the file path manually to import the modules from another directory.


To import python file from current directory you can simply do as follows:

test.py:
from .mytest import * 

here dot represent current directory where test.py is located

Because whenever you import without dot operator - python find that package in site-packages directory; but in your case you dont have mytest module in your python site-packges directory that is why its throwing error module not found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜