python: 从不同的文件目录导入模块,文件夹名有空格

python: import module from different file directory, with folder names having spaces

我已经知道这将被(在我看来是错误的)标记为重复,因为 this or this,我已经看过了。

但是我的问题不同,无法通过那里找到的答案解决。

所以我在 C:\Users\Me\Desktop\My folders\Projects\My stuff\Last Folder\some_file.py 中有一个 python 脚本。由此我想在同一目录中导入一个文件,即在 Last Folder 内,为此我只使用 import file。 但是,然后我在 C:\Users\Me\Desktop\My Other Folder\My Other Projects\ My Other Stuff\other_file.py 中有一个文件。如您所见,文件夹中有空格。

现在,在上面两个 link 的第二个中,我们被告知如何导入名称中带有空格的 python 文件,一个 python 文件,而不是文件夹!

在第二个 link 中,用户只想使用 os 打开一个文件。

我反而想导入它。我该怎么做?当我尝试

import Users.Me.Desktop.My Other Folder.My Other Projects.My Other Stuff.other_file.py

它说 "invalid syntax" 因为有空格。我该如何解决这个问题?

import sys
sys.path.append('\Users\Me\Desktop\My folders\Projects\My stuff\Last Folder\some_file.py.') 
# From this I want to import a file in the same directory, i.e. inside Last Folder, for this I just use import file. However, then I have a file in C:\Users\Me\Desktop\My Other Folder\My Other Projects\ My Other Stuff
from other_file import *

我需要将 utils.py 文件导入 Google Colab Notebook,这对我有用,结合了 Surya 和 here.

的解决方案
from google.colab import drive
drive.mount('/content/drive')

import sys
sys.path.append("/content/drive/My Drive/Colab Notebooks")  # note the absolute path
print(sys.path)

from utils import a, b, c