How to import a file anywhere you like in Python

conditions

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.

Folder structure

├─python
| main.py
|
| ├─code
| | |mnist.py
|
| ├─dataset
| | |activation_function.py

To-do list

  1. I want to ** import ** ** mnist.py ** in ** main.py ** !!
  2. I want to ** import ** ** activation_function.py ** in ** mnist.py ** !!
  3. I want to ** import ** ** main.py ** in ** mnist.py ** !!

What I want to do 1

I want to ** import ** ** mnist.py ** in ** main.py ** !!

Method 1

main.py


import sys
sys.path.append("code")
import mnist

Method 2

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

What I want to do 2

I want to ** import ** ** activation_function.py ** in ** mnist.py ** !!

Method 1

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

Method 2

mnist.py


import sys
sys.path.append("")
# sys.path.append("./")But possible
# sys.path.append(".")But possible
import dataset.activation_function

Method 3

mnist.py


import sys
sys.path.append("dataset")
import activation_function #Become a little beautiful

What I want to do 3

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

end

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

How to import a file anywhere you like in Python
How to create a JSON file in Python
How to get a stacktrace in python
How to read a CSV file with Python 2/3
How to clear tuples in a list (Python)
How to embed a variable in a python string
Summary of how to import files in Python 3
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to read a file in a different directory
Note: [Python3] Convert datetime to a string in any format you like
How to develop in Python
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
[Python] How to expand variables in a character string
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
[Work efficiency] How to change file names in Python
How to execute a command using subprocess in Python
Run a Python file with relative import in PyCharm
How to drop Google Docs in one folder in a .txt file with python
[Python] How to do PCA in Python
How to slice a block multiple array from a multiple array in Python
How to import Python library set up in EFS to Lambda
How to run a Python file at a Windows 10 command prompt
How to import NoteBook as a module in Jupyter (IPython)
How to collect images in Python
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to use SQLite in Python
Create a binary file in Python
How to define multiple variables in a python for statement
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
[Python] How to import the library
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
How to use Mysql in python
How to get a list of built-in exceptions in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
[Python] How to change EXCEL file saved in xlsb to xlsx
[Python] How to output a pandas table to an excel file
How to import NoteBook as a module in Jupyter (IPython)
How to handle Japanese in Python
How to create a config file
How to read text by standard input or file name specification like cat in Python
[Small story] A painstaking measure when you have to execute a function before import in Python
How to pass arguments to a Python script in SPSS Modeler Batch
How to make a string into an array or an array into a string in Python
What to do if you get a minus zero in Python
[Introduction to Python] How to output a character string in a Print statement
How to check the memory size of a variable in Python
How to get a string from a command line argument in python
How to create a heatmap with an arbitrary domain in Python
[Python] When you want to use all variables in another file
[Introduction to Python] How to use the in operator in a for statement?
How to specify a .py file to load at startup in IPython 0.13
How to check the memory size of a dictionary in Python