English 中文(简体)
正在将自定义模块导入到 jupyter 笔记本
原标题:Importing custom module into jupyter notebook
Yes, I know this is a recurrent question but I still couldn t find a convincing answer. I even read at https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html but could not find out how to solve the problem: I m running python 3.6 project that includes jupyter (ipython) notebooks. I want the notebook to import a custom local helpers.py package that I will probably use also later in other sources. The project structure is similar to: my_project/ │ ├── my_project/ │ ├── notebooks/ │ └── a_notebook.ipynb │ ├── __init__.py # suppose to make package `my_project` importable │ └── helpers.py │ ├── tests/ │ └── helpers_tests.py │ ├── .gitignore ├── LICENSE ├── README.md ├── requirements.txt └── setup.py When importing helpers in the notebook I get the error: ----> 4 import helpers ModuleNotFoundError: No module named helpers I also tried from my_project import helpers and I get the same error ModuleNotFoundError: No module named my_project I finally (and temporarily) used the usual trick: import sys sys.path.append( .. ) import helpers But it looks awful and I m still looking for a better solution
问题回答
One can tell python where to look for modules via sys.path. I have a project structure like this: project/ │ ├── src/ │ └── my_module/ │ ├── __init__.py │ └── helpers.py ├── notebooks/ │ └── a_notebook.ipynb ... I was able to load the module like so: import sys sys.path.append( ../src/ ) from my_module import helpers One should be able load the module from wherever they have it.
Here I could find several solutions. Some of them are similar to the ones answered before: https://mg.readthedocs.io/importing-local-python-modules-from-jupyter-notebooks/index.html
If you move the notebooks directory out one level, and then explicitly import your module from the package, that should do it. So your directory would look like this: my_project/ │ ├── my_project/ │ ├── __init__.py │ └── helpers.py ├── notebooks/ │ └── a_notebook.ipynb ... and then your import statement within the notebook would be: from my_project import helpers.
Try the following line: from my_project.helpers import what_you_need This line should also work: import my_project.helpers
This worked for me. import sys MODULE_FULL_PATH = ///my_project/my_project sys.path.insert(1, MODULE_FULL_PATH) from my_module import helpers
I think you need a __init__.py module in the notebooks/ directory. I haven t really used Jupyter notebooks before so I could be wrong. You may also need to try changing your import statement to: import .. helpers to indicate that the import statement is for a local package that is located in the parent directory of the Jupyter notebook.
If you are on a Unix/Linux system another elegant solution may be creating a "soft link" to the module file helpers.py that you would like to use. Change to the notebooks directory and create the link to the module file this way: cd notebooks; ln -fs ../my_project/helpers.py . This "soft link" is essentially a pointer (a shortcut) to the original target file. With the link in place you will be import your module file as usual: import helpers
It is probably too late for answer, but the solution for me was pretty much simple. My problem was that I first created a project in Jupiter, and then add e helpers.py with my finctions. So basically your Jupiter notebook won t see it untill you restart the Kernel in your Jupiter Notebook. Make sure you did that, and once you do, your imports strat works no problems.
I am not able to import -> !pip install pythonocc-core in jupyter notebook. Please advise




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签