admin管理员组文章数量:1391981
src
|
----folder1
|------file.py
----folder2
|------file2.py
Now If I try to access file
from file2
I get No Module Named Error
.
from folder1.file import *
What can I do to fix this?
src
|
----folder1
|------file.py
----folder2
|------file2.py
Now If I try to access file
from file2
I get No Module Named Error
.
from folder1.file import *
What can I do to fix this?
Share Improve this question asked Mar 15 at 20:08 Rishabh AgarwalRishabh Agarwal 334 bronze badges 4 |2 Answers
Reset to default 0So I'm back - restructuring the code fixed the problem:
src
|
----setup.py
----primary_folder
|
----__init__.py
----folder1
|------file.py
|------__init__.py (empty)
----folder2
|------file2.py
|------__init__.py (empty)
The code for the setup.py
is :
from setuptools import setup, find_packages
setup(
name="primary_folder",
version="1.0",
packages=find_packages(),
author="John Doe",
install_requires=[
# Add other dependencies
],
)
Then ran the command pip install -e .
to make the package primary_folder
editable and use it (whenever new code is added - need to rerun this) and import
statements become from primary_folder.folder1.file import *
Hope this helps people with similar problems. Python folder usage is problematic.
from ..folder1.file import *
Try this
本文标签: quotNo Module namedquot errorPython File organizationStack Overflow
版权声明:本文标题:"No Module named" error - Python File organization - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744604724a2615282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
..
in the import statement to indicate a parent directory. Tryfrom ..folder1.file import *
. – John Gordon Commented Mar 15 at 20:11__init__.py
file in each directory you're importing stuff from, to tell Python it's a package. – joanis Commented Mar 15 at 22:17__init__.py
didn't work. I had to restructure my code a lot and then use asetup.py
to basically create a package and then usepip install -e .
to make the package editable @JohnGordon @joanis – Rishabh Agarwal Commented Mar 16 at 23:53