AttributeError: 'ModuleSpec' object has no attribute 'load_data_wrapper' - python

This is a bit long so bear with me.
I am trying to learn both Python and Linux and am very new to both. I am currently doing some reading on deep learning from the following:
http://neuralnetworksanddeeplearning.com/chap1.html
I am attempting to import the mnist_loader package to use the associated data for testing the script that was previously written. However, upon typing import mnist_loader into the Linux command line, I was given the following:
"the program 'import' can be found in the following packages:"
at which point it listed some packages. Because I'm new to Linux and I don't have admin privileges, I decided to go a route that I understood better; that is to create a new python script and simply use the import command within (which has worked in all previous attempts).
I created a python script and tried import mnist_loader and received the following error:
"ModuleNotFoundError: No module named 'mnist_loader'"
I then checked my C drive and found that the file was indeed there. Here is a link to the Git repository where the files may be found:
https://github.com/MichalDanielDobrzanski/DeepLearningPython35
Next I moved on to trying to directly input the path to the file as follows:
import importlib.util
mnist_loader = importlib.util.spec_from_file_location("mnist_loader",r"C:\Users\XXXXXX\Documents\neural-networks-and-deep-learning-master\neural-networks-and-deep-learning-master\src\mnist_loader.py")
training_data, validation_data, test_data = mnist_loader.load_data_wrapper()
However, this produced the following error:
"AttributeError: 'ModuleSpec' object has no attribute 'load_data_wrapper'"
Note: the last line is used to collect the necessary data from the nist files.
I am running out of thoughts at this point and would love some feedback on all my "wrongdoings" up till now.
Thanks in advance!
P.S. It is worth noting that the book uses a package designed for Python 2.X whereas I am using 3.6. The readme provided by the book file location mentioned a different location where a Python 3.6 version could be found which is what I am going with.

Been a while since I have worked with Python, but I have some ideas as to what would cause the specific errors you are seeing. First I would suggest setting a PYTHON_PATH environment variable with the path you have that contains the module you want to import, this does not require administrator privileges fortunately. As for the load_data_wrapper attribute, you might have to do a from from mnist_loader import * to import all the functions inside the mnist_loader module.

Related

Matlab cannot import Python package (getting namespace module)

I have a problem with this line in MATLAB:
py.importlib.import_module('asammdf');
It imports the library but it returns the following:
'<module 'asammdf' (namespace)>'
once I try to initialize MDF object (which is this lib part) with:
MDF = py.asammdf.MDF();
I will get the following error message:
Unable to resolve the name py.asammdf.MDF
Which by my understanding it means that the Python I use in MATLAB was able to track the 'asammdf' folder hence the namespace module, but it was not able to detect anything further.
I will note that I already tried the same approach on another machine and it worked fine. The imported module was returning:
'<module 'asammdf' from 'C:\\Program Files\\Python38\\lib\\site-packages\\asammdf\\__init__.py'>'
and I was able to create MDF. It's strange because Paths are the same on boths computers.
I also already reinstalled Python with all libraries, still the same.
I think that it might be important - I can use this library outside MATLAB e.g. in python console.
All thoughts appreciated.

Import local copy of python module

I have a basic question about how to package a python script with a local copy of a module, in this case the yaml module, so that i can run it on another machine and not have to download/install the module.
I tried taking the 'yaml' directory from the repo on github, putting it in the root of my python script, and simply running an import statement, but that doesn't seem to work; when i run import yaml or from yaml import load, my script fails with exception unexpected token '='.
I'm sure that I'm missing something obvious, but I wasn't able to find a clear answer by looking around for a bit. Any help would be appreciated.

PUDB Error: <no source code available>

I'm new to pudb. It run fine for the most-part, but when I try to enter a library it apparently doesn't recognize, I get the following message:
<no source code available>
If this is generated code and you would like the source code to show up here,
add it to linecache.cache, like
import linecache
linecache.cache[filename] = (size, mtime, lines, fullname)
You can also set the attribute _MODULE_SOURCE_CODE in the module in which this function
was compiled to a string containing the code.
I've tried importing 'linecache' and the 'cache' attribute is a dictionary. I've tried creating an entry for the missing module a few times with no success.
Can someone please give an example on an easier and/or practical way to add an unrecognized module to pudb?
The way it worked for me is the following.
I got this message when a piece of code was executed that has been generated on-the-fly. I tracked down the location where the code was generated, and added:
import linecache
linecache.cache[__file__] = (len(source), 0, source, __file__)
(where the source variable corresponds to the generated source)
What I observed after, was that in pudb interactive mode, in the stack list a new item appeared. This new item was preceeding the one that throws the <no source code available> message.
When I navigate on this new item, I can see the generated source.
This answer is inspired from this GitHub issue. A possible workaround is to use the full path of the script.
Quite simply, instead of python script.py, use python /full/path/to/script.py.
This works because some libraries may use chdir to change directories and when that happens, pudb cannot locate the script, resulting in the error.
TIP: To save some keystrokes, you can simply type python $PWD/script.py instead of typing in the full path.
I was having this issue and in my case it was because the package was being installed as .egg, you can circumvent that installing with pip install -e foo_package

How do I register a custom bundle with zipline?

I am following the tutorial here:
http://www.prokopyshen.com/create-custom-zipline-data-bundle
and trying to set up a custom bundle to get price from custom, non US financial assets. I am stuck on the line that says:
Advise zipline of our bundle by registering it via .zipline/extension.py
My extension.py file is located in the .zipline/ directiory and has the following code:
from zipline.data.bundles import register
from zipline.data.bundles.viacsv import viacsv
eqSym = {
"CBA"
}
register(
'CBA.csv', # name this whatever you like
viacsv(eqSym),
)
I don't get what it means to register the bundle via .zipline/extension.py though? I thought it might mean to just run the extension.py file from my terminal via a:
python extenion.py
but that fails and says:
ImportError: No module named viacsv
How do i register this bundle?
I also followed this tutorial and I must confess this part is a little confusing.
First of all, I don't think it's necessary to run:
$ python extension.py
The error message you get probably comes from the fact that Python cannot find the viacsv.py file in sys.path (the places where it looks for modules, etc.). In the tutorial you mentioned, it's not really clear what to do with this file. As far as I am concerned, I just saved the viacsv.py file in my local site-packages directory. As I am on Linux I put it there ~/.local/lib/python2.7/site-packages but it might different for you. You can run the following python script to find out:
import sys
for dr in sys.path:
print dr
Then I just substituted from zipline.data.bundles.viacsv import viacsv with from viacsv import viacsv in extension.py.
I suspect you might be looking for the wrong place for the extension.py file.
For windows machine, the file is under "~\.zipline\extension.py". In my case, it's under "C:\Users\XXXX\.zipline\extension.py".
I had been looking at zipline folder under conda's site-packages folder, and couldn't find it. Then created an extension.py myself wondering why it's not called.
Check a related post here https://www.quantopian.com/posts/zipline-issue-while-creating-custom-bundle-to-bring-yahoo-data.
Same issue here, #Gillu13 pointed me to this solution.
I installed zipline through conda. So zipline is installed in
home/me/anaconda3/envs/krakenex/lib/python3.6/site-packages
in there you will find zipline/data/bundles and you can put viacsv.py in there...
then
from zipline.data.bundles.viacsv import viacsv
works

how python import packages with multiple copies

I was believing that when importing a package, it would search from sys.path and use the first hit for import. However, it seems not true:
import mpl_toolkits
print(mpl_toolkits.__path__)
And it outputs:
['/Library/Python/2.7/site-packages/matplotlib-1.5.0-py2.7-macosx-10.11-intel.egg/mpl_toolkits', '/usr/local/lib/python2.7/site-packages/mpl_toolkits']
Can someone please explain to me how exactly python looks for packages if it is installed multiple times in the machine (in different location searchable by sys.path)? Or a pointer to relevant reference would be good.
when you import a module, python uses PYTHON PATH (system variable that contains a list of folders) and loops to search for importable module.
Python will test if it is a package (folder containing init.py) or a module (*.py). it will stop on first module found if no module is found python raises an import error

Categories

Resources