最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Why did I need to add an empty __init__.py, and why was a later sys.path entry overriding an earlier one? - Stack Overf

programmeradmin4浏览0评论

I created a test project with the root directory named test_import to reproduce the problem. When I ran the following code to print the sys.path list:

import sys
for path in sys.path:
    print(path)

The output was:

path/test_import/src
/root/anaconda3/envs/pytorch/lib/python39.zip
/root/anaconda3/envs/pytorch/lib/python3.9
/root/anaconda3/envs/pytorch/lib/python3.9/lib-dynload
/root/anaconda3/envs/pytorch/lib/python3.9/site-packages

Here's the structure of my project:

test_import
└── src
    ├── datasets
    │   ├── file1.py
    └── main.py

In the main.py file, I have the following import statement:

import datasets.file1

However, when I directly run python main.py, I get the error ModuleNotFoundError: No module named 'datasets.file1'

After investigating, I found that there is a datasets file in the /root/anaconda3/envs/pytorch/lib/python3.9/site-packages directory.

I managed to solve the problem in two ways:

  1. By adding an __init__.py file in the datasets directory of my project.
  2. By renaming the datasets directory in my project to avoid the conflict.

Now, I have a few questions:

  1. In Python 3, the __init__.py file is not considered necessary. So, what is the specific role of an empty __init__.py file in Python 3?
  2. The path/test_import/src path in the sys.path list comes before the /root/anaconda3/envs/pytorch/lib/python3.9/site-packages path. Why does the file in the latter path interfere with the normal import from the former path?

Moreover, I conducted an additional test. I added the statement sys.path = sys.path[:-1] in the main.py file of my original test project to remove the /root/anaconda3/envs/pytorch/lib/python3.9/site-packages path from the sys.path list. After doing this, I was able to import datasets.file1 without any issues.

Environment Details:

  • Python version: 3.9.18 with Anaconda
  • Operating System: Linux
发布评论

评论列表(0)

  1. 暂无评论