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
..
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