简体   繁体   中英

from os import path import sys sys.path.append(path.abspath('../ProgA'))

I don't understand what the below code does.

from os import path
import sys
sys.path.append(path.abspath('../ProgA'))
from os import path

imports path from the os module this is used in line 3

import sys

imports the sys module

sys.path.append(path.abspath('../ProgA'))

This line is kinda tricky so ill break it down

path.abspath() takes a relative path (from the file executing the code) and gives you the absolute path to that

for example if you are in the directory /home/user/adi/documents and you call path.abspath('../ProgA'), the.. means you go up a directory a then look for a directory called ProgA so the final path will be /home/user/adi/ProgA

sys.path is just the place where python looks for imports this is by default in you site packages but you edit this variable

TLDR; the code append the directory../ProgA to your sys.path so that you can import modules from the directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM