Cannot import Python modules from sibling directory in Jupyter Notebook running in VS Code (os.chdir and sys.path.append do not work) #16691
Replies: 2 comments 1 reply
-
Please could you enable logging as follows:
|
Beta Was this translation helpful? Give feedback.
-
I do exactly what you're trying to do all the time, with a bit extra to allow me to run a python file located in a completely different directory, (from within any workspace) as if I were in that other file's directory. I usually just copy and paste this block between projects, modifying the path, and it has been serving me well so far: import os
import sys
os.system("clear")
RootDirectory = "/home/username/absolute/path/to/the/workspace/project/root/directory"
os.chdir(RootDirectory)
for SearchPath in [SP for SP in sys.path if (RootDirectory in SP)]: sys.path.remove(SearchPath)
sys.path.insert(0, f"{RootDirectory}/relative/path/to/your/modules/library/data")
sys.path.insert(0, f"{RootDirectory}/relative/path/to/your/second/directory/for/modules/library/data")
#(and just keep adding more relative paths as needed, in ascending order of priority)#
sys.path.insert(0, os.getcwd())
if ((1)):
print("="*100)
print(f"Current Working Directory (CWD):\n'{os.getcwd()}'\n")
print("Search Paths:")
for i, PathLine in enumerate(sys.path): print(F"[{i+1}]: '{PathLine}'")
print("="*100) The only real difference I'm seeing is that I'm inserting my new (abs) paths at the front (where they will get searched first), while you're appending them to the back (where they will be searched last). Also, I just generally wouldn't suggest changing the CWD using a relative path string in Jupyter NB, because without resetting the kernel, every time you run that cell, the CWD will take a further step back; it's not like a regular python file, where the CWD resets every time you run the code. Another thing you could try I guess, is to write out the absolute path to the module yourself, or print out the I recon just printing out the code below should hopefully help you debug your pathing issues (that is if it is an issue with pathing): if ((1)):
print("="*100)
print(f"Current Working Directory (CWD):\n'{os.getcwd()}'\n")
print("Search Paths:")
for i, PathLine in enumerate(sys.path): print(F"[{i+1}]: '{PathLine}'")
print("="*100) Another potential issue might be just that Pylance isn't seeing the module (prior to PYTHONPATH being changed during runtime), but that shouldn't throw an error; it should only just put a yellow line under the import and not give you the tooltips for the objects retrieved from that module. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am running Jupyter Notebooks inside Visual Studio Code using the official Jupyter extension.
My project structure is as follows:
In
analysis.ipynb
, I would like to importmy_module
using either:or:
However, I consistently get the following error:
I have read the [official VS Code Jupyter documentation](https://code.visualstudio.com/docs/datascience/jupyter-notebooks), but it does not cover anything about import paths or how
sys.path
is handled.Here are the things I have tried:
This does not work. The import still fails.
sys.path
:This also does not work. The import fails with the same
ModuleNotFoundError
.In a normal Python script (outside of a notebook), these methods usually work for me. But in this case, inside a Jupyter Notebook running in VS Code, they are not working.
I would like to understand:
Any advice or clarification would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions