Jupyter Notebook Unable to Load Module mlwpy - python

I am completely new to programming in general and I do not understand very much about computers. A very similar question was asked previously, however I do not understand anything what was said.
Background: I am attempting to run the first line of code as described in the book Machine Learning with Python for Everyone by Mark Fenner on the Jupyter browser notebook.
This is the code:
In [1]:
from mlwpy import*
%matplotlib inline
Running the code returns this error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-3489a3655c97> in <module>
----> 1 from mlwpy import *
2 get_ipython().run_line_magic('matplotlib', 'inline')
ModuleNotFoundError: No module named 'mlwpy'
From my understanding we need a module mlwpy.py and this module can be found at this link thanks to the author of the book here; https://github.com/mfenner1/mlwpy_code/blob/master/mlwpy.py
However, I do not understand what exactly I am supposed to do with this information. Someone mentioned putting the file in the same directory as our Jupyter notebook.
This is where I am confused, what does that mean exactly? I copied and pasted the text onto Jupyter and then saved it as mlwpy.ipynb in the binder, however the problem continues to remain the same.
If someone could explain step by step what I need to do, that would be greatly appreciated.

mlwpy is not a Python's built-in library, nor an external library you can install with pip or conda. It is a custom python file you would like to import.
To run from mlwpy import *, you need to have a python script named mlwpy.py in the same directory as your Jupyter notebook file.
So download raw.githubusercontent.com/mfenner1/mlwpy_code/master/mlwpy.py and put it in the same directory as your Jupyter notebook. The downloaded file should be named as mlwpy.py.

You also have to check that you're in the correct working directory if running Jupyter. You can import os and print(os.getcwd()) to see if you're in the right place. If not, either put the file in the working directory, or cd to the correct working directory before launching Jupyter.

Related

How to manage pytests’ `tests` folder name collision with jupyter extensions

I’m in the process of writing unit tests for my software.
I wrote some helpers for my tests. For convenience I would like to use them in a jupyter notebook.
When I try to import them inside of the notebook though, I get an error.
from tests import helpers
->
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-12-d8ba72c24738> in <module>
----> 1 from tests.helpers import some_helper
ModuleNotFoundError: No module named 'tests.helpers'
Digging a little, I found out that importing tests actually imports this folder as a module:
'/my_project_path/.venv/lib/python3.8/site-packages/IPython/extensions/tests'
The folder contains test_autoreload.py and test_storemagic.py, which are tests for extensions I use.
Here’s my question, how do I properly manage this conflict? I would like to keep those extension installed, and I would like to keep the name tests for my folder, as it is the convention when working with pytest.
I installed those extensions with pip. Did I miss an option to ignore the tests when installing or something?
Thanks! :)
Not really a solution but some hints here in case they help you:
I was having the same issue today and getting a bit crazy. Not in jupyter but int the ipyton shell. As jupyter uses ipython I guess it will be the same issue.
IPython has a module called tests
within the IPython/extensions folder, and this folder is added is added to the search path when using ipython instead of python.
This are my paths from a python shell (using sys.path):
['',
'/usr/local/lib/python37.zip',
'/usr/local/lib/python3.7',
'/usr/local/lib/python3.7/lib-dynload',
'/usr/local/lib/python3.7/site-packages']
And the ones from an ipython shell:
['/usr/local/bin',
'/usr/local/lib/python37.zip',
'/usr/local/lib/python3.7',
'/usr/local/lib/python3.7/lib-dynload',
'',
'/usr/local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/site-packages/IPython/extensions',
'/home/dmontaner/.ipython']
So, if your are in iptyhon or in a notebook and you import tests or from tests import something, the interpreter will search in:
'/usr/local/lib/python3.7/site-packages/IPython/extensions'
So far all is as expected. But I also had as you my own tests module in the same directory where my ipython session was running. What should happen then is that my own tests module should be imported first. But it was not and I had the same error as you.
If I would remove the 'IPython/extensions' from the path doing sys.path.remove(.../IPython/extensions') then I was able to import my own module.
I did uninstall ipyton and ipython_genutils and, using python -m pytest in the command line (on a .py file, not a notebook) I was still having a slightly similar issue this time about dash trying to load the IPython. The weird thing is that in my project I am not using dash but flask and dash is not a dependency of pytest anyway I think.
So, I uninstalled ipyton, ipython_genutils, dash and plotly and then I could run pytest importing from my own tests module.
I reinstalled the 4 libraries again and the problem was solved! And now I can import from my tests module even from within a jupyter notebook.
I guess the message is that there is some buggy setup or dependency among all those libraries (and may be some others). Try to reinstall latest versions or recreate your virtual environment in case it helps.
Sorry that I could not figure out what was the exact problem... but I hope this helps you.
The long-term fix for these kind of naming conflicts is to put all your code, including your test code, into a project package, such that you imports look like
import my_project.tests.helpers instead of import tests.helpers.
By choosing a unique name for your project, you define your own namespace and avoid naming conflicts.

PyCharm does not run – Project Interpreter problems

First of all, I want to note that I've read a lot of links related to my problem.
I have some problems with running the code when I want to import a module.
Here is an example of the code:
import numpy as np
Then PyCharm gives me an exception:
Traceback (most recent call last):
File "/Users/Alexander 1
2/PycharmProjects/Coursera/Week1/Vectors/Vecors.py", line 1, in
import numpy as np
ModuleNotFoundError: No module named 'numpy'
Process finished with exit code 1
I have read this and did all what was written. However, it hasn't been working. I've reloaded the PyCharm – no result.
Help me, please! I'm using MacOS 10.13.5, PyCharm 2018.1.4 CE.
You probably have created a new project using virtualenv, read about it here.
It basically creates a new project specific environment, so the libraries inside won't cause a conflict with python2 and python3 packages. So, inside Pycharm, open terminal by pressing alt+f12 , which will open a terminal inside Pycharm, and install numpy usually by pip install numpy
I don't know why, but when I rebooted the computer and created a new project, this problem has disappeared.

Jupyter Notebook: Import .ipynb file and access it's method in other .ipynb file giving error

I am fairly new to Jupyter Notebook. I have played around with it for a while. But this was my first time trying to import another notebook into my main class.
For reference, I am using Anaconda 4.3.1 and Python v2.7.
I am trying to replicate what I did in my python project to jupyter notebooks. It requires importing other .ipynb files (translated from the original .py files) into it to use relevant methods as desired.
For this, I followed the directions as given on Jupyter Nbviewer Steps Link which I found through my preliminary search on the following stack Question. It gave me some idea but didn't help me after one stage.
I will walk you through the steps I took and the sample program I tried.
Created a small .ipynb file abc.ipynb as follows
def prt_n(str):
print(str)
if __name__ == '__main__':
prt_n("in abc")
Created an .ipynb file to import Jupyter Notebook from the Jupyter link given above. Say, importer.ipynb.
Run importer.ipynb
import abc
str="Hello Me"
Test step abc.__name__ results in abc as output.
abc.prt_n(str) throws following error
*---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-2fb88a43c9e5> in <module>()
----> 1 abc.prt_n(str)
AttributeError: 'module' object has no attribute 'prt_n'*
I was hoping that the answer will be Hello Me.
Later, I want to actually create a myMain.ipynb file and in that I want to include 2-3 such notebooks and call their methods with required parameters.
One such example can be a file efg.ipynb as follows:
import abc
a="Hello Notebook"
abc.prt_n(a)
I also want to make sure if there is any other way to do this?
Note: I have already checked sys.executable and sys.path. Both have the same python path value.
Any sort of help is welcome!
Simple way to use ipynb files in jupyter note book are as follows:
1) Install import-ipynb
pip install import-ipynb
2) Import import_ipynb in jupyter notebook. Then import ipynb file as you import .py file
import import_ipynb
from test import print_name
print_name("your name")
**
Link to sample files on Drive
**
Ok. So, after some struggle and looking around on Internet, and finally found a solution that worked for my sample case.
Firstly, this is the stackoverflow question that was the most helpful to me. Mohideen and Tyhela's answer was the actual solution and not the one with most number of votes.
So, what I did was, I made a folder by the name module and placed all my .ipynb files there. Plus, I created an __init__.py file in that module by using touch __init__.py command such that the import can register it as a valid module. Those guys have given a detailed explanation for it which seems legit.
Then from my working directory I ran the following commands:
str = "Hello Me"
import test.abc as tabc
tabc.prt_n(str)
that gave me Hello Me in the output.
And for,
`import test.efg as tefg`
I got
importing Jupyter notebook from test/efg.ipynb
Hello Note
As my desired output.
I hope this will be of help to others who land up on similar problem.
If you have a better approach then I will appreciate if you can share that with me.
Thank you :)

Can only import win32com.client with IDLE. What extra work do i need to set up pywin32?

I have a Tkinter program that i use for time management. I decided to have it scan my inbox in outlook to check emails for tags, and then based on the tag, add it to my list of tasks to do for the night.
The code i wrote works just fine, but i'm getting an error when I import win32com.client. I can import it in IDLE, but it is having problems importing when i try to run the code with a .bat file, or double clicking the .py file.
I have found several people with similar problems, but from what i can tell, it's having problems with win32api module, or pywin32
Traceback (most recent call last):
File "my_program_filename.py", line 1, in <module>
import win32com.client
File "c:/Python27/lib/site-packages/win32com/__init__.py", line 5, in <module>
import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found
I'm really confused. When i get the sys.path it's the same with IDLE as it is running from the .py file with the exception of an added "c:/users/username/desktop/timer" for my .py file.
I'm really lost and haven't had to mess with the system path, but I'm not able to figure out what i need to do to fix this.
What do I need to do to get pywin32 working so I can use win32com.client?
Any help is greatly appreciated. Thanks!
IIRC, the problem was attempting to link to the debug build. I think i had to dupe the release build and rename it to be debug or something.
try building release and see if it "just works". if so, you have a direction to explore
this is an issue of not having the correct paths in the sys.path. If you make sure that all of the pywin32 folders are in the sys.path(If you check in IDLE it can show that the folders are included even when they aren't?!?!?).
You also have to make sure you run your code from inside your Python directory or it will fail to import win32api. I also found that if you do anything in a function that uses pywin32 and you accidentally misspell the function when you call it, the whole import fails without telling you your function is misspelled. You can also navigate to the /Python27/Lib/site-packages/win32com/client folder and run makepy.py to make sure the right Object library is installed.
When you run makepy.py, you select the COM object you want to use and it creates packages specific to what you want to use. I don't fully understand why this is, but once i did this and ran my file from the Python folder it worked! There is a more in depth explanation on how to get this working right here.
I found this link to be the solution rather than the win32com/client as indicated above: win32com import error python 3.4
in my case typing in cmd:
python C:\Python27\Scripts\pywin32_postinstall.py -install
I hope this helps

running pronterface in python: import error: no module named

I am a complete beginner to programming, python, pronterface and 3d printing so unsurprisingly I am having a few difficulties.
I have now managed to download pronterface and get it running on my computer, i think i followed the download instructions correctly and have skeinforge downloaded also in the printrun folder.
My problem is that when I try to open or load a .stl file I get the following:
Skeining C:\Users\James\Downloads\70mm_Faberdashery_Yoda-Lite_less_robe.stl
Skeinforge execution failed.
Traceback (most recent call last):
File "C:\Users\James\Desktop\3d printer software\pronterface\Printrun-d482c66\pronterface.py", line 940, in skein_func
from skeinforge.skeinforge_application.skeinforge_utilities import skeinforge_craft
ImportError: No module named skeinforge_application.skeinforge_utilities
However the file is in the right place I think:
C:\Users\James\Desktop\3dp\pronterface\Printrun-d482c66\skeinforge\skeinforge application\skeinforge utilities\skeinforge_craft
Can you try to explain in plain language any ideas to fix this as I am a total beginner and don't even really know what a module import error is.
Someone on the reprap forms was having a similar issue, the recommendation there is to run skeinforge from the command line.
To solve the problem more satisfactorily, you'd need to check what path is available to python and make sure that the path to the skeinforge module is in your system path or the python path.

Categories

Resources