Reputation: 10948
Lets say this is my project structure :
codes
- pre.py
training
- model.py
Both folder have empty __init__.py
inside them, and they are at the root folder of my project.
Suppose I want to call pre.py
from the model.py
:
import sys
sys.path.insert(0, "../codes")
from pre import *
However the code above give me ModuleNotFoundError
.
How can I fix this error?
Upvotes: 0
Views: 2367
Reputation: 2271
You can create a setup.py file at the outermost level. So your directory tree may look like this
|_project_name
|_codes
|_pre.py
|___init__.py
|_training
|_models.py
|___init__.py
|___init__.py
|_setup.py
Then in the setup.py do something like this
from distutils.core import setup
from setuptools import find_packages
requires = [
'six>=1.10.0',
]
if __name__ == "__main__":
setup(
name="project-name",
version="0.0.1",
packages=find_packages(),
author='Your Name',
author_email='[email protected]',
install_requires=requires,
description='The API to fetch data from different sources',
include_package_data=True,
)
And finally from the outermost directory use python setup.py install
it will install your package and then you will be able to easily do from project_name.pre import *
in models
Also consider using a virtualenv
for this kind of things.
Hope this helps.
Upvotes: 3
Reputation: 8537
try using full path of your code folder
import sys
sys.path.insert(0, "fullpath")
from pre import *
Upvotes: 1