SoFunction
Updated on 2024-11-13

Explain how to import a file as an absolute or relative path in Python.

1, in Python to the relative path or absolute path to import the file or module method

Today in the debugging code, the program has been prompted by the absence of the module, has been very puzzled, because I have been importing files with absolute path to import. It is reasonable to say that there will not be a module can not be found.

Finally a careful analysis of the entire directory structure of the code revealed the problem.

Here's a description of the day in order

Let's look at the imported code first:

existdemo——The following two import operations are performed in the file
# First place
from  import GCForest
from .config_utils import load_json
# Second place
config = load_json("demo_mnist") # Inside is passed the location of the file,This function reads the contents of the configuration file

Then look at the structure of the catalog

目录的具体结构 

In this example, demo_mnist,py is the main file. The first thing it will import is a file in a directory that is a subdirectory of the parent directory of its current directory.

So importing this file as an absolute path would befrom import GCForest

Importing this file as a relative path isfrom .. import GCForest

The second import is another file in its unified directory, so you can directly import the
So the modified import code is:config = load_json("demo_mnist")

2. By the way, review the concept of relative and absolute paths

Absolute path is the real existence of the file's path, is to start from the root directory of the hard disk (disk drive letter), to carry on the level of directory pointing to the file.

A relative path is a hierarchical directory pointing to a referenced resource file based on the current file.

The following identifiers are commonly used to indicate the current directory and the parent directory of the current directory

... / indicates the directory one level up from the directory where the current file is located.
. / indicates the directory where the current file is located (can be omitted)
/ denotes the root directory of the current site (the hard disk directory to which the domain name is mapped)

It is possible to verify the above marking methods in Python:

The directory structure of the test:

目录的结构信息

import os
path1=('.') # Indicates the absolute path of the folder you are currently in
print(path1)
path2=('..') # Indicates the absolute path to the folder one level up from the folder you are currently in.
print(path2)
# D:/PycharmProjects/mnistCheck/
D:\PycharmProjects\mnistCheck
D:\PycharmProjects

3. Learn more about the use of Python.

is the set of paths for python's search module, a list, as follows:

[
'D:\\PycharmProjects\\mnistCheck', 'D:\\PycharmProjects\\mnistCheck', 'D:\\PycharmProjects\\KerasDataSettry', 'C:\\ProgramData\\Anaconda3\\envs\\tensorflow\\', 'C:\\ProgramData\\Anaconda3\\envs\\tensorflow\\DLLs', 'C:\\ProgramData\\Anaconda3\\envs\\tensorflow\\lib', 'C:\\ProgramData\\Anaconda3\\envs\\tensorflow', 'C:\\ProgramData\\Anaconda3\\envs\\tensorflow\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\envs\\tensorflow\\lib\\site-packages\\Sphinx-1.6.3-py3.'
]

This can be used in a python environment: .

(‘The name of your module')。
(0,'Name of the module')

Add the relevant paths, but the paths you added yourself will automatically disappear after exiting the python environment!

The implementation in the code is as follows:

import sys
('C:\Users\Administrator\Desktop\ExperimentAss\gcForest-master')

4, finally understand python in different levels of directory import module method (focus)

There is a folder /home/a, there is a module called, how do I import it into a program?

Method I.    (The first case in the code analyzed at the beginning.)

import sys; 
(“/home/a/”) 
import b

Method II:

Add the __init__.py file to the directory, where you can write the code to be executed when importing, or just leave it blank.

import 

Method Three:

from  import * 

Prerequisites home, a include __init__.py i.e.: both the current directory and the parent directory of the file to be imported should have theinit.py file

Python's method of including modules in subdirectories is relatively simple; the key is to be able to find the path to the module file within it.

A few common scenarios are described below.

(1) Main program and module program in the same directory.

For example, the following program structure.

– src 
  |–  
  |– 

If you import module mod1 in your program, use import mod1 or from mod1 import *;.

(2) The directory where the main program is located is the parent (or ancestor) of the directory where the module is located

For example, the following program structure.

– src 
|–  
|– mod2 
  | –  
– 

To import the module mod2 into your program, create an empty __init__.py file in the mod2 folder (you can also customize the output module interface in this file); then use from mod2.mod2 import * or import mod2.mod2.

(3) The main program imports modules in the upper directory or modules under other directories (level)
For example, the following program structure.

– src 
  |–  
  |– mod2 
   |–  
  |– sub 
    | –  
  –  

If you import modules and in the program. First you need to create the __init__.py file under mod2 (same as (2)), you don't need to create the file under src. Then call it as follows.

The following programs are executed in the directory where the program file is located, such as in cd sub; after the execution of python

Rather, executing python after cd src; ; does not guarantee the success of executing python sub/ in the src directory.

import sys 
(“..”) 
import mod1 
import mod2.mod2

(4) As can be seen from (3), the key to importing a module is to be able to find the path to a specific module based on the value of an environment variable. Only the above three simple cases are described here.

Summary:

By summarizing, you can find that when the file you want to import is in the same directory as your current file, you can just import this file name directly.

When the file or directory you want to import is not in the same directory as your current file, you need to jump to the parent directory of the file you want to import, and then one level by one, use the dot to connect the directory or file that you have walked through, and then you can do it As for how to jump to the parent directory, it is more common to add the parent directory to the system path, and then use the dot to search until you reach the module you want to import. As for how to jump to this parent directory, it is more common to add the parent directory to the system path, and then use the dot to search one level until you reach the module you want to import.

This is the entire content of this article.