如何在新目录下添加程序时修复所有目录错误?

问题描述 投票:0回答:1

我对编程很陌生,我想学习如何使用我自己以外的程序:我的重点是为这个程序设计一个网络界面:https://github.com/teplinsky-maxim/spotify-playlist-下载器.git.

我将程序克隆到我的项目中,但在运行时无法在 main.py 文件中找到“核心”目录。 VS Code 还说它无法找到目录的定义,尽管它能够找到它导入的类的定义(这很奇怪)。

程序可以在项目外运行

导入语句:

import argparse
import asyncio
import time

from core.downloader.soundloader.soundloader import SoundLoaderDownloader
from core.logger.main import Logger
from core.playlist.extractor.spotify.spotify import PlaylistExtractor


错误:

...
  File "C:\Users\ppwan\My shit\Giraffe\spotifyv4\giraffe\spotify-playlist-downloader\core\downloader\soundloader\soundloader.py", line 13, in <module>
    from core.downloader.base import DownloaderBase
ModuleNotFoundError: No module named 'core'

-> 我做的第一件事是在核心目录中实现一个 innit.py 文件。没用。

-> 安装的程序名称中有连字符,将其克隆并用下划线替换连字符。有很多问题,太害怕改变任何东西。 (暂定)

-> 尝试使用绝对路径进行转换,但 python 再次不喜欢连字符。没用。

-> 使用 importlib,但所有文件,甚至是核心目录中的文件,都需要更改其导入语句。会花太长时间。

最好的解决方案是什么?

python oop subdirectory
1个回答
0
投票

您可以对程序执行以下几行:

import os
import sys
sys.path.insert(0, os.getcwd())

这会将您的工作目录插入

Path
.

© www.soinside.com 2019 - 2024. All rights reserved.