Jupyter Notebook-无法从其他文件夹导入python函数 [英] Jupyter notebook - can't import python functions from other folders

查看:1387
本文介绍了Jupyter Notebook-无法从其他文件夹导入python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jupyter笔记本,我想使用计算机中其他文件夹中的本地python函数. 当我确实导入这些功能时,出现此错误: "ModuleNotFoundError:没有名为'xxxxxxxxxxxxx'的模块

I have a Jupyter notebook, I want to use local python functions from other folders in my computer. When I do import to these functions I get this error: "ModuleNotFoundError: No module named 'xxxxxxxxxxxxx'

  • 我正在使用anaconda作为python解释器

推荐答案

您可以使用sys将路径添加到本地模块/python文件.

You can add a path using sys to your local module/python file.

import sys
sys.path.append("/path/to/file/")  # path contains python_file.py

import python_file

如果要通过将模块添加到Anaconda路径来获得更永久的解决方案,请参见cord-kaldemeyer https://stackoverflow的先前答案. com/a/37008663/7019148 .为了完整起见,下面复制了以下内容:

If you want a more permanent solution by adding module to Anaconda path, see previous answer from cord-kaldemeyer https://stackoverflow.com/a/37008663/7019148. Content copied below for completeness:

我在Anaconda论坛中找到了两个问题的答案:

I found two answers to my question in the Anaconda forum:

1.)将模块放入站点包中,即始终位于目录$ HOME/path/to/anaconda/lib/pythonX.X/site-packages中 sys.path.这也应该通过创建符号链接来起作用.

1.) Put the modules into into site-packages, i.e. the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages which is always on sys.path. This should also work by creating a symbolic link.

2.)将.pth文件添加到目录$ HOME/path/to/anaconda/lib/pythonX.X/site-packages.可以命名为 任何东西(它必须以.pth结尾). .pth文件只是一个 用换行符分隔的目录的完整路径名列表,该目录包含 将在Python启动时添加到您的路径中.

2.) Add a .pth file to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pth file is just a newline-separated listing of the full path-names of directories that will be added to your path on Python startup.

两者都很简单,我选择了第二个选项,因为它是 更加灵活.

Both work straightforward and I went for the second option as it is more flexible.

***更新:

3.)在软件包的文件夹中创建setup.py并使用pip install -e/path/to/package进行安装,这是最干净的选项 从我的角度来看,因为您还可以看到所有使用的安装 点列表.

3.) Create a setup.py in the folder of your package and install it using pip install -e /path/to/package which is the cleanest option from my point of view because you can also see all installations using pip list.

还是谢谢!

这篇关于Jupyter Notebook-无法从其他文件夹导入python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆