Can't seem to find a straightforward answer to this question. All I want to know is how to import the file I saved under my \scripts
directory.
I added the path to sys.path
...
import sys
sys.path.insert(0, "C:\\my_Stuff\\data_science\\scripts")
I added a __init__.py
file under my \scripts
directory, which is the same directory where my tree.py
file is.
I run...
import tree as tr
...but am still getting the error.
Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .
A relative import specifies the resource to be imported relative to the current location—that is, the location where the import statement is. There are two types of relative imports: implicit and explicit. Implicit relative imports have been deprecated in Python 3, so I won't be covering them here.
My answer is probably late but the following is working for me.
Let's say i have "my_file.py" in the directory "folder":
import sys
sys.path.append('path\\to\\folder')
from my_file import ...
Hope this can help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With