Notebook import Jupyter - python

I am trying to import a class i created in one Jupyter notebook, to a different notebook and i get an error message saying the following:
from newclassattempt import MyClass
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-4337d8f762d7> in <module>
----> 1 from newclassattempt import MyClass
ModuleNotFoundError: No module named 'newclassattempt'
How can i get around this and import my class?

Please note that for the import to work we need the module to be a Python File, meaning with the extension .py.
The Jupyter Notebooks are not your plain python files and are thus not imported by normal python machinery
You can actually import a Jupyter Notebook using Hooks. A lot of it is detailed in the documentation. But I also found another method
In the thisnotebook.ipynb (the one you are working on), all you need to do is use these commands:
import import_ipynb
import newclassattempt
For this to work ensure that you install:
pip install import-ipynb
You can also use your from newclassattempt import MyClass
(The above was taken from this)

Update: I found a fully functional solution.
As mentioned in other comments, the class needs to be stored as a .py file in the same folder as the jupyter notebook. the following code then worked:
import newclassattempt
ob = newclassattempt.MyClass(1)
where newclassattempt is the name of the .py file, ob is the object created and MyClass is the class in the .py file. The reason my class couln't be imported is because I had not referenced the original file name

Related

Why can't Idle import from .py files created after it was launched?

I can't explain this phenomena, other than to assume that Idle somehow works off of a snapshot of the filesystem, taken at launch-time.
Repro steps:
create a myLib.py file with (e.g.):
#!/usr/bin/env python3
pre_launch_str = "Pre-launch!"
# post_launch_str = "Post-launch!"
launch Idle (from the containing folder)
from myLib import pre_launch_str works as expected: the string is imported/usable
Keep Idle running/open
[from another application/terminal] modify myLib.py to include a new object (e.g.) post_launch_str
from myLib import post_launch_str will throw an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'post_launch_str' from 'myLib' (/home/myUser/myLib.py)```
Anyone know what the cause of this is?
Linux (zsh) + Python 3.10 inthe example above, but I've noticed this long ago (~Python3.5 and on MacOS too)
This has nothing to do with IDLE. When you've already put a module into scope (regardless of which names you imported from it), Python will never reload that module. It assumes the module's code has not changed and will use the in-memory version.
If you're planning to modify the module live during development, you'll need to use importlib to reload it. You need a reference to the module itself. If you imported the module name as import myLib, then myLib will do. Otherwise, take a class or function that you imported and get its __module__ (so pre_launch_str.__module__ in your example). Then take that and call reload on it.
from importlib import reload
reload(pre_launch_str.__module__)

ModuleNotFoundError: No module named 'utils.image_classifier'

trying to run it this error is coming repeatedly nothing is working
the following is the error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-19-86f6d90695b7> in <module>()
2 import numpy as np
3
----> 4 from utils.image_classifier import ImageClassifier, NO_FACE_LABEL
5
6 # Color RGB Codes & Font
ModuleNotFoundError: No module named 'utils.image_classifier'
I'm guessing you need to install some package or other. The import statement is looking for a python module (script file) called "image_classifier.py" in a sub-folder called "utils" somewhere in your path. That module needs to contain the class definition for ImageClassifier objects and what looks like a constant (variable assignment) called NO_FACE_LABEL.
I did a quick google but I couldn't find the package that might contain that module. Are you aware of any pre-requisites for your code, things you need to have installed before you start?
Try:
pip install utils
If you've done it and it is still not working, you need to perform a search for "utils.py", and run your script on the same folder as in the "utils.py".

Jupyter Notebook does not import any module

ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-c1d07d468637> in <module>
----> 1 import requests
2
ModuleNotFoundError: No module named 'requests'
I first installed a module and was wondering why it was not working.
Then I tried with modules that ought to be installed, like pandas and requests.
On all modules, I get the same issue.
Then I checked if the modules really are not installed, or if they are not in the proper folder
After that, I uninstalled and reinstalled anaconda
Nothing worked so far. I appreciate any help
Jupyter error message:
Pip installed modules:
I think you are installing your modules on a vitualenv and the Jupyter notebook is running outside the virtualenv.
This happened to me once.
Maybe you forgot to append the path to your modules to sys.path.
Example from within your notebook, if u want to import some selfwritten module from some relative location:
import sys
sys.path.append("../../../")
sys.path.append("../../")
# Rest of your code goes here, for example import $MODULE_NAME
Then you can import $MODULE_NAME (so, use the proper modulename of your desired module) iff. that module is in ../../ or in ../../../.
HTH. :-)

I can't import a module in Python/Windows

I'm having problem when I trying to import a custom module I have which is very simple but I'm always getting:
Traceback (most recent call last):
File "demo_module1.py", line 12, in <module>
import mymodule
ModuleNotFoundError: No module named 'mymodule'
I have tried to set environment variables:
set PYTHONHOME=C:\Software\python-3.7.4
set PYTHONPATH=%PYTHONPATH%;C:\pyproys\test
Everything is located here: 'C:\pyproys\test'
The only way it works is if I add it directly in the code "But I don't want to do it in every single script I have so don't want to maintain it in that way".
import sys
sys.path.append('C:\pyproys\\test')
print(sys.path)
Here is the script I'm trying to run:
demo_module1.py
import mymodule
mymodule.greeting("Jonathan")
'mymodule.py' is in the same folder as 'demo_module1.py'
I'm expecting the code to run fine by just executing:
python demo_module1.py
Can someone please point me out what I'm doing wrong?
Try to find the directory /lib/site-packages in your Python folder in C drive and paste your module in that folder and then restart the system. Hope it'll solve your issue.

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

Categories

Resources