简体   繁体   中英

Fail to use imports in jupyter notebook in vscode

I have a pretty straightforward code that run smoothly with Python 3.7:

import academic_data_settings as local_settings
import pandas as pd
import glob
import os

def get_all_data():
    all_files = glob.glob(os.path.join(local_settings.ACADEMIC_DATA_SOURCE_PATH, "*.csv"))
    df_from_each_file = [pd.read_csv(f) for f in all_files]
    concatenated_df = pd.concat(df_from_each_file, ignore_index=True)
    return concatenated_df

if __name__ == "__main__":
    raw_data = get_all_data()
    print(raw_data)

However, it is pretty hard to visualize the data in the pandas dataframe.

In order to view the data, I found the following article on how to use Jupyter notebook directly from VSCode: https://devblogs.microsoft.com/python/data-science-with-python-in-visual-studio-code/

In order to be able to see the Python interactive window, I needed to turn the code into a jupyter cell:

#%%
import academic_data_settings as local_settings
import pandas as pd
import glob
import os

def get_all_data():
    all_files = glob.glob(os.path.join(local_settings.ACADEMIC_DATA_SOURCE_PATH, "*.csv"))
    df_from_each_file = [pd.read_csv(f) for f in all_files]
    concatenated_df = pd.concat(df_from_each_file, ignore_index=True)
    return concatenated_df

if __name__ == "__main__":
    raw_data = get_all_data()
    print(raw_data)

As soon as I try to run or debug the cell, I get an exception at the first line:

import academic_data_settings as local_settings...
ModuleNotFoundError: No module named 'academic_data_settings'

I believe that the cell evaluation only send the code of the current cell. Is that correct? Is there a way to get the import to work correctly? I wouldn't like to end up writing Jupyter notebooks and then copy over the code to what will end up being the 'production' code.

I had a similar issue. I could import modules in IPython in the vscode terminal but not in the vscode interactive window (or jupyter notebook).

Changing the .vscode/settings.json file from

{
"python.pythonPath": "/MyPythonPath.../bin/python"
}

to

{
"python.pythonPath": "/MyPythonPath.../bin/python"
"jupyter.notebookFileRoot": "${workspaceFolder}"
}

resolved it for me.

Jaep. I'm a developer on this extension and I think I know what is happening here based on this comment:

@Lamarus it is sitting next to the file I run

The VSCode python interactive features use a bit different relative loading path versus jupyter. In VSCode the relative path is relative to the folder / workspace that you have opened as opposed to jupyter where it is relative to the file. To work around this you can either change your path to academic_data_settings to be relative to the opened folder, or you can set the Notebook File Root in the setting to point to the location that you want for this workspace to be the working root. We have a bug to support using the current file location for the notebook file root here if you want to upvote that.

https://github.com/microsoft/vscode-python/issues/4441

I think I have been running into a similar problem. I'm fairly new to python and recently I've been trying to teach myself how to build basic libraries so I can reuse code. I followed several websites on how to build your library and what to put in your init .py files. I was testing what I'd done in a Jupyter notebook I had open in VS Code. No matter what I seemed to do I kept getting error messages like yours. When I tried running the same notebook in Jupyter Lab my experimental library worked just like in the tutorials. I also tried an interactive session in the terminal and again I got the expected behaviour, everything working fine. I had a slight preference for VSCode over Jupyter Lab because of the slicker text prediction and variable explorer. However if I can't get imports for my libraries to work it does put me off. I might still do a bit more experimentation seeing if I can get VS Code to import as it should. I don't like the idea of having the imports written differently to work when run in VS Code to Jupyter Lab though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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