简体   繁体   English

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

[英]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从第 3 行使用的 os 模块导入路径

import sys

imports the sys module导入 sys 模块

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 path.abspath() 采用相对路径(来自执行代码的文件)并为您提供该路径的绝对路径

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例如,如果您在目录 /home/user/adi/documents 中并调用 path.abspath('../ProgA'),则 .. 表示您在 go 上一个目录,然后查找一个名为 ProgA 的目录,因此最终路径将是 /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 sys.path 只是 python 查找导入的地方,这是默认情况下在您的站点包中,但您编辑此变量

TLDR; TLDR; the code append the directory../ProgA to your sys.path so that you can import modules from the directory代码 append 目录../ProgA 到您的 sys.path 以便您可以从目录中导入模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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