It seems that some versions of Python can and cannot do it. I've given some examples, so if you make a mistake, try another method.
├─python
| main.py
|
| ├─code
| | |mnist.py
|
| ├─dataset
| | |activation_function.py
I want to ** import ** ** mnist.py ** in ** main.py ** !!
main.py
import sys
sys.path.append("code")
import mnist
I referred to "Set sys.path if you want to import your own Python module". It didn't work in my environment. It may work in some environments.
main.py
import sys
sys.path.append("python/code")
import mnist
I want to ** import ** ** activation_function.py ** in ** mnist.py ** !!
I referred to Deep Learning from scratch. It didn't work in my environment. It may work in some environments.
mnist.py
import sys, os
sys.path.append(os.pardir) #Settings for importing files in the parent directory
import dataset.activation_function
mnist.py
import sys
sys.path.append("")
# sys.path.append("./")But possible
# sys.path.append(".")But possible
import dataset.activation_function
mnist.py
import sys
sys.path.append("dataset")
import activation_function #Become a little beautiful
I want to ** import ** ** main.py ** in ** mnist.py ** !!
** I didn't understand this !! Please let me know if possible !! ** First of all, importing the upper file from the lower file seems to be useless as a folder structure, so it may be that you should review the folder structure! By the way, I tried the following, but it didn't work in my environment.
mnist.py
import sys
sys.path.append("../")
import main
I don't think many of the methods I've introduced will work with Python2. Let's use Python3 I think that some of them don't work because of the difference in the handling of the parent folder. Is this the difference between the OS (Mac and Windows)? It may be verified later.
I feel that the "file" part of the title is not appropriate, so please let me know in the comments if there is something to replace it m (_ _) m
Recommended Posts