Import py file in another directory in Jupyter notebook

馋奶兔 提交于 2020-11-30 04:19:51

问题


My question is related to this. I am using Python 3.6 in Jupyter Notebook. My project directory is /user/project. In this directory I'm building a number of models and each has it's own folder. However, there is a common functions.py file with functions that I want to use across all models. So I want to keep the functions.py file in /user/project but be able to call it from an .ipynb file in /user/project/model1, /user/project/model2, etc... How can I do this?


回答1:


There is no simple way to import python files in another directory. This is unrelated to the jupyter notebook

Here are 3 solutions to your problem

  1. You can add the directory containing the file you with to import to your path and then import the file. like this
import sys  
sys.path.insert(0, '/path/to/application/app/folder')

import file
  1. You can create a local module by having an empty __init__.py file in the folder you want to import. There are some weird rules regarding the folder hierarchy that you have to take into consideration.

  2. You can create a module for the file you wish to import and install it globally.




回答2:


Assuming you have a folder name Jupyter and you wish to import modules (employee) from another folder named nn_webserver.

visualizing it:

do this:

import sys
import os

module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path+"\\nn_webserver")

from employee import motivation_to_work

see additional information here from @metakermit




回答3:


I would suggest to install functions.py as a package in your virtual environment. There are some benefits of this:

  1. You can access functions.py file from any iPython notebook located in any place, but at the given environment (kernel).
  2. Once you changed any function in functions.py file, you don't need to reload your iPython notebook again and again. It will automatically reload every change.

This is the way how it can be done:

  • Create setup.py file (https://docs.python.org/2/distutils/setupscript.html) in your project folder
  • Activate your virtual environment, go to your project location, and use this command pip install -e .
  • Then, in your iPython notebook:

    %load_ext autoreload

    %autoreload 1

    %aimport yourproject.functions

    from functions import *

That's it!




回答4:


I've solved this problem in the past by creating a symbolic link in the directory where the Jupyter notebook is located to the library it wants to load, so that python behaves as if the module is in the correct path. So for the example above, you would run the following command once per directory inside a Jupyter cell:

!ln -s /user/project/functions.py functions.py

and then you could import with

import functions

Note: I've only tried this on Linux and Mac Os, so I can't vouch for Windows.




回答5:


In addition to the answer from adhg, I recommend using Pathlib, for compatibility between Linux/Windows/WSL paths formats:

Assuming the following folder structure:

.
├── work
|   ├── notebook.ipynb
|   └── my_python_file.py
├── py
|   ├──modules
|   |    ├──__init__.py # empty
|   |    └──preparations.py
|   ├──__init__.py # empty
|   └── tools.py
├──.git
└── README.md

To load tools.py or preparations.py in my_python_file.py (or in notebook notebook.ipynb):

import sys
from pathlib import Path

# in jupyter (lab / notebook), based on notebook path
module_path = str(Path.cwd().parents[0] / "py")
# in standard python
module_path = str(Path.cwd(__file__).parents[0] / "py")

if module_path not in sys.path:
    sys.path.append(module_path)

from modules import preparations
import tools
...


来源:https://stackoverflow.com/questions/49264194/import-py-file-in-another-directory-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!