简体   繁体   English

无法在 vscode 的 jupyter notebook 中使用导入

[英]Fail to use imports in jupyter notebook in vscode

I have a pretty straightforward code that run smoothly with Python 3.7:我有一个非常简单的代码,可以在 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.但是,很难将 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/为了查看资料,我找到了下面这篇关于如何直接从VSCode使用Jupyter notebook的文章: 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:为了能够看到 Python 交互 window,我需要将代码转换为 jupyter 单元格:

#%%
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.我不想最终编写 Jupyter 笔记本,然后将代码复制到最终成为“生产”代码的地方。

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).我可以在 vscode 终端的 IPython 中导入模块,但不能在 vscode interactive window(或 jupyter notebook)中导入模块。

Changing the .vscode/settings.json file from更改.vscode/settings.json文件

{
"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 @Lamarus 它位于我运行的文件旁边

The VSCode python interactive features use a bit different relative loading path versus jupyter. VSCode python 交互功能使用与 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.在 VSCode 中,相对路径相对于您打开的文件夹/工作空间,而不是相对于文件的 jupyter。 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.要解决此问题,您可以将 Academic_data_settings 的路径更改为相对于打开的文件夹,或者您可以将设置中的 Notebook File 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 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.我对 python 还很陌生,最近我一直在尝试自学如何构建基本库,以便我可以重用代码。 I followed several websites on how to build your library and what to put in your init .py files.我关注了几个关于如何构建库以及在init .py 文件中放入什么内容的网站。 I was testing what I'd done in a Jupyter notebook I had open in VS Code.我正在测试我在 VS Code 中打开的 Jupyter 笔记本中所做的事情。 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.当我尝试在 Jupyter Lab 中运行相同的笔记本时,我的实验库就像在教程中一样工作。 I also tried an interactive session in the terminal and again I got the expected behaviour, everything working fine.我还在终端中尝试了交互式 session ,我再次得到了预期的行为,一切正常。 I had a slight preference for VSCode over Jupyter Lab because of the slicker text prediction and variable explorer.由于更流畅的文本预测和变量资源管理器,我对 VSCode 比 Jupyter Lab 稍有偏好。 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.我可能还会做更多的实验,看看我是否可以让 VS 代码按应有的方式导入。 I don't like the idea of having the imports written differently to work when run in VS Code to Jupyter Lab though.我不喜欢在 VS Code 中运行到 Jupyter Lab 时以不同方式编写导入以工作的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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