I am pretty new to ipython and still learning and liking its functionality. Trying to write a module with common tasks so users of a notebook can load my custom module and save typing common code.
Here is a sample mymodule.ipy:
import mymodule as mymod
mymod.func():
wd=%pwd
return wd
When I test it in the notebook:
import mymodule as my
---------------
ImportError Traceback (most recent call last)
<ipython-input-1-1036e4702cf4> in <module>()
----> 1 import mymodule as my
ImportError: No module named mymodule
But when I rename the module file as mymodule.py, the module load fails with error message denoting it cannot recognize the pwd magic:
File "mymodule.py", line 2
wd=%pwd
^
SyntaxError: invalid syntax
My question is how do I write a module with ipython magics and other ipython functionality in it which I can load from notebooks? I had the impression that I can put ipython code into a file with .ipy extension and can load it from ipython. Am I missing something?
"%run mymodule.ipy" as pointed in my comment serves my need for now. Posting it as answer to close this question. Feel free to post more if you have a "module" approach.
Related
I'm using Thonny version 3.3.13 on Windows 10 to program Raspberry Pi Pico.
The main program is main.py. I have no issues with it (examples are working), except any local imports.
I'm following this tutorial.
It is not duplicated, as I've searched and tested many version of import on StackOverflow and many other websites for hours.
My file structure:
sd_card_read
|-main.py
|-lib
|-__init__.py
|-SDCard.py
My main.py file:
import sys
print(sys.path)
import SDCard
#... the rest of the code
The error I'm getting is:
['', '.frozen', '/lib']
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
ImportError: no module named 'SDCard'
How can I solve the import?
Notes:
I tried appending '.' and '/' to sys, it does not work. e.g sys.path.append('/')
I tried different versions of import, no luck. e.g from lib import SDCard
While Thonny allows you to run a file opened from your local computer, it ONLY allows importing modules from its own internal storage.
For me, this is confusing.
I ran "Save copy..." on all my module files, chose "Raspberry Pi Pico" and entered the filename manually.
Maybe there is another way of doing this in Thonny, as this is my first time using MicroPython on RPi Pico.
Try
from lib import SDCard
It works like that because lib is the package and not SDCard. SDCard is just a file in the package.
Following the instructions on MicroPythonTestFramework/MicroPythonPkg, I can successfully run micropython.efi (using VS2015x86 instead of VS2017).
However importing the uefi module as given in the example on the above link fails:
FS0:\EFI\Python\> micropython.efi
MicroPython v1.9.4 on 2018-07-30; uefi version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import Lib.Uefi.uefi as uefi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'Lib.Uefi'
MicroPythonDxe.efi is in the same folder as micropython.efi.
How do I do to compile the uefi module into MicroPythonDxe.efi?
In order to successfully import the Python UEFI module, you must have the UEFI Python library on your device and accessible.
The modules you are looking for are located in MicroPythonPkg/MicroPythonDxe/Lib/Uefi.
The device file system will end up looking something like below
FS0:\EFI\Python\Lib\Uefi\uefi.py
\features.py
\msr.py
\protocols.py
\register.py.... etc
I am trying to compile a Python package that I recently migrated from Python2 to Python3.
When running the source code in Python3 it works as expected, so do the source and compiled versions for Python2, but when I cythonize and compile the Python3 package the resulting binaries throw this error when importing a certain module:
dynamic module does not define module export function (PyInit_NKPD)
EDIT:
When I then close the Python interpreter, open it again and import the same model it works.
Interestingly when I put the code on my local drive it imports fine, but when I put it on my server and import it from there I get the above error. And to make matters worse, sometimes it imports fine from the server as well if I just wait a while and try again:
lockjaw:controller frank$ python3.7
Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import common
added to sys.path: /opt/ohufx/transfer/NUBRIDGE_COMPILE/osx/NKPD/src
trying to import model.NukepediaDB...
NukepediaDB imported
trying to import model.NKPD...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/controller/common.py", line 15, in init controller.common
import model.NKPD # this does not import
ImportError: dynamic module does not define module export function (PyInit_NKPD)
>>> import common
added to sys.path: /opt/ohufx/transfer/NUBRIDGE_COMPILE/osx/NKPD/src
trying to import model.NukepediaDB...
trying to import model.NKPD...
NKPD imported
model.NKPD always seems to import fine, but model.NKPD seems to be the stumbling block, even though this may be a red herring. It almost behaves like sometimes the server connection drops out after the first import, though in that case the other incarnations (Python 2/source code) would throw errors as well (not to mention a whole lot of other things would break in my office).
I have reduced the package to three modules with nothing but import and print statements which can be found here (including the resuling C++ and binary files).
The structure is very simple:
Where common is the module I import to test by cd-ing into the src/controller folder, opening Python3.7 and importing common. Common then imports model.NukepediaDB and model.NKPD, the latter causing the trouble.
I cythonized the python files with this line:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 py2cpp.py
Then compiled with this:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 setup.py build_ext --inplace
I would love to understand why the import sometimes fails when run from the server, as I have to distribute this package to others and need to ensure it runs reliably.
Thanks,
Frank
hi guys recently i've encounter this problem in google colab, and i dont know how to solve this problem
import h2o
h2o.init
data_train = "https://drive.google.com/open?id=14OYzA93IkuKPlkdvXs9Od53HyopgSQ2u"
data_train_df = h2o.import_file(path=data_train)
i following a book that suggest these following codes, but somehow it wont work.
The h2o package is not automatically installed in Google Colab. If you attempt to run import h2o without installing it, you will get the following error.
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-accdebc0c7de> in <module>()
----> 1 import h2o
ModuleNotFoundError: No module named 'h2o'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------
Based on the error message you can then start a new code block and run !pip install h2o to install the package. Then, running import h2o should be successful.
One more thing to keep in mind is that you need to include parentheses to call a function. Running h2o.init will return the description <function h2o.h2o.init>. Try h2o.init() to initialize an h2o cluster.
Edit: The “not connected to a cluster” means you have not initialized a cluster. Try the h2o.init() command again, but using parenthesis so that the function is called and not printed.
I have gone through various ways to import a ipython file in jupyter. I am new to it so i have no idea how to import the file.
Code
import SupplyDelay.ipynb as we do in python
Error:
ModuleNotFoundError Traceback (most recent call last)
in ()
3 import numpy as np
4 from sklearn.model_selection import train_test_split
----> 5 import SupplyDelay.ipynb
ModuleNotFoundError: No module named 'SupplyDelay'
File Hierarchy:
Folder abc:
SupplyDelay
Forecast
Forecast should import SupplyDelay
Have you tried using the %run magic designed to run a notebook from within a notebook? Use like so:
%run SupplyDelay.ipynb
If that doesn't work or had undesired side effects, you can also follow the instructions in the docs: http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/Importing%20Notebooks.html
Here i have created two .ipynb file
1. Master.ipynb
2. Child.ipynb
The Master.ipynb looks something like this
Here i have defined few functions.
Now i have created the Child.ipynb in the same directory as Master.ipynb, the following snip shows how to call Master.ipynb from the Child.ipynb
Hope, this solves your problem.