Reputation: 3561
I already know this will be (wrongly, in my opinion) labelled as duplicate because of this or this, which I've already looked at.
However my problem is different, and cannot be solved with the answers found there.
So I have a python script in C:\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\other_file.py
. As you can see the folders have spaces.
Now in the second of the two links above we are told how to import a python file with spaces in the name, a python file, not a folder!
In the second link, the user wants to just open a file using os.
I instead want to import it. How do I do it? When I try
import Users.Me.Desktop.My Other Folder.My Other Projects.My Other Stuff.other_file.py
it says "invalid syntax" because of the spaces. How can I solve this?
Upvotes: 0
Views: 2348
Reputation: 575
I needed to import a utils.py
file into a Google Colab Notebook and this worked for me, combining the solution from Surya and 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
Upvotes: 0
Reputation: 84
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 *
Upvotes: 1