About Graphlab library importing - python

In Ubuntu 14.04, I have installed Graphlab based on https://dato.com/download/install-graphlab-create-command-line.html and it seems to be working fine.
However, I receive this error when trying to use a recommender module:
import graphlab
from graphlab.recommender import ranking_factorization_recommender
In the first line, graphlab is imported without any error. However, the second line causes this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-34df81ffb957> in <module>()
----> 1 from graphlab.recommender import ranking_factorization_recommender
ImportError: No module named recommender
How can the problem be solved? Thanks

It's just a namespace issue. recommender actually lives in the `toolkits module, so this should work:
import graphlab
from graphlab.toolkits.recommender import ranking_factorization_recommender

Graphlab has already imported everything for you in their __init__.py file.
Just do:
from graphlab import ranking_factorization_recommender
from graphlab import <any_other_recommender>
Here is a snippet of graphlab.__init__.py file:
from graphlab.util import get_runtime_config
from graphlab.util import set_runtime_config
import graphlab.connect as _mt
import graphlab.connect.aws as aws
from . import visualization
import os as _os
import sys as _sys
if _sys.platform != 'win32' or \
(_os.path.exists(_os.path.join(_os.path.dirname(__file__), 'cython', 'libstdc++-6.dll')) and \
_os.path.exists(_os.path.join(_os.path.dirname(__file__), 'cython', 'libgcc_s_seh-1.dll'))):
from graphlab.data_structures.sgraph import Vertex, Edge
from graphlab.data_structures.sgraph import SGraph
from graphlab.data_structures.sarray import SArray
from graphlab.data_structures.sframe import SFrame
from graphlab.data_structures.sketch import Sketch
from graphlab.data_structures.image import Image
from graphlab.data_structures.sgraph import load_sgraph, load_graph
from graphlab.toolkits._model import Model, CustomModel
import graphlab.aggregate
import graphlab.toolkits
import graphlab.toolkits.clustering as clustering
import graphlab.toolkits.distances as distances
...

Related

cannot import name 'MSELossFlat' from 'fastai.layers'

I am running the code https://github.com/thiagodma/Pytorch_exs/blob/master/MultiTaskLearning/multitask_age_gender_ethnicity_resnet34.ipynb on Colab.
As I write:
from fastai import *
from fastai.vision import *
from fastai.layers import MSELossFlat, CrossEntropyFlat
from torchvision import transforms
import warnings
warnings.filterwarnings("ignore")
files_train = get_image_files("utkface_aligned_cropped/UTKFace")
files_valid = get_image_files("utkface_aligned_cropped/crop_part1")
I get the error:
ImportError Traceback (most recent call last)
<ipython-input-4-07ceae9afcad> in <module>()
1 from fastai import *
2 from fastai.vision import *
----> 3 from fastai.layers import MSELossFlat, CrossEntropyFlat
4 from torchvision import transforms
5 import warnings
ImportError: cannot import name 'MSELossFlat' from 'fastai.layers' (/usr/local/lib/python3.7/dist-packages/fastai/layers.py)
This is probably because you are referring to a code snippet that used the fastai v2 library. In the newest fastai, the MSELossFlat function can be imported at fastai.losses.
But since you are trying out an old code snippet, it's best to reproduce the execution enviroment of that code to get an expected result. I would recommend trying to install fastai v2 on your environment and execute the code again.

Cannot import name 'functional_datapipe' from 'torch.utils.data'

When I am running datasets_utils.py from '/usr/local/lib/python3.7/dist-packages/torchtext/data/datasets_utils.py' in Google Colab, the following error occurs even with the most updated versions of Python packages:
ImportError: cannot import name 'functional_datapipe' from 'torch.utils.data' (/usr/local/lib/python3.7/dist-packages/torch/utils/data/init.py)
Are there any solutions to solve such errors, as I could not find functional_datapipe even in the official torch.utils.data documentation? The following is excerpt from datasets._utils.py in the Google Colab environment
import functools
import inspect
import os
import io
import torch
from torchtext.utils import (
validate_file,
download_from_url,
extract_archive,
)
from torch.utils.data import functional_datapipe, IterDataPipe
from torch.utils.data.datapipes.utils.common import StreamWrapper
import codecs
It might be available only on torchdata.datapipes

ImportError in Jupyter Notebook

I am trying import a module in Jupyter and it is not working:
import alyn
ImportError Traceback (most recent call last)
<ipython-input-8-8e9535ea4303> in <module>()
----> 1 import alyn
~\Anaconda3\envs\tracx\lib\site-packages\alyn\__init__.py in <module>()
1 """ Import required modules"""
----> 2 from deskew import *
3 from skew_detect import *
ImportError: No module named 'deskew'
I don't quite understand why, since the package in question has a correct init.py file:
whose contents are:
""" Import required modules"""
from deskew import *
from skew_detect import *
What am I missing?
P.S.
This is all taking place on Windows 10.
Well, I've figured it out!
Turns out that the package I was trying to import is written in Python 2 and its init file is using the relative import mechanism. However, I am working in Python 3 and relative import is no longer supported in it. The init file can be made to work in Python 3 by adding a . in both lines, like this:
""" Import required modules"""
from .deskew import *
from .skew_detect import *
I think this should be backward compatible with Python 2.

do I have a circular import in python

I'm trying to port over a python project from v2.x, to v3.x
one of the major changes to python was the import system.
I am now seeing an error when trying to load my python notebook as follows
package/
__init__.py
bh_tsne.py
Collect Samples.ipynb //imports utils.list_all_files, sees error
Error Output
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-1339232cd15c> in <module>()
1 import numpy as np
2 from os.path import join
----> 3 from utils.list_all_files import list_all_files
4 from multiprocessing import Pool
/~/AudioNotebooks/utils/__init__.py in <module>()
4 from . import show_array
5 from . import make_mosaic
----> 6 from . import bh_tsne
7 from . import normalize
8 from . import mkdir_p
ImportError: cannot import name 'bh_tsne'
strangely.. I think the problem is a circular dependence.. but bh_tsne doesn't rely on any utilities.. could the circularity be coming from my utils.list_all_files and then the __init__.py ?
bh_tsne imports
from argparse import ArgumentParser, FileType
from os.path import abspath, dirname, isfile, join as path_join
from shutil import rmtree
from struct import calcsize, pack, unpack
from subprocess import Popen
from sys import stderr, stdin, stdout
from tempfile import mkdtemp
from platform import system
from os import devnull
import numpy as np
import os, sys
import io
Edit
Is that redundant os.path join perhaps the root cause?
I ended up just upgrading the wrapper that was used in one project from it's source upstream project. The original owner had done upgrades.
https://github.com/lvdmaaten/bhtsne/blob/master/bhtsne.py
and the import worked after that as
import utils.bhtsne as bhtsne
I found out that bh_tsne seems NOT to work with python 3. Also another version (Multicore TSNE) just work with python 2.7 as well

python from . import fails

Trying caffe python examples from: http://caffe.berkeleyvision.org/tutorial/interfaces.html gives me error:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
caffe_root = '/opt/caffe'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-5-18cb333d5c1b> in <module>()
7 sys.path.insert(0, caffe_root + 'python')
8
----> 9 import caffe
...
...
/usr/lib/python2.7/site-packages/scipy/signal/__init__.py in <module>()
272 from __future__ import division, print_function, absolute_import
273
--> 274 from . import sigtools
275 from .waveforms import *
276 from ._max_len_seq import max_len_seq
ImportError: cannot import name sigtools
Apparently the sigtools import fails, but I can't figure out why. The /usr/lib/python2.7/site-packages/scipy/signal contains all files:
$ ls -1 /usr/lib/python2.7/site-packages/scipy/signal/sign*
/usr/lib/python2.7/site-packages/scipy/signal/signaltools.py
/usr/lib/python2.7/site-packages/scipy/signal/signaltools.pyc
In general, how python process directives like this, specifically what dot is resolved to if my working directory was completely different from the location where sigtools package is located?
from . import sigtools
As stated here:
`from ... import` vs `import .`
"from . import sigtools" imports the main module "." (which is "signal") than imports the object/module sigtools. If "." has been already imported it rely on that structure.
I think that this can be tricky in case you have 2 modules with the same name in the python import path: the interpreter imports the first one found and never imports the second one. If the second one has more modules than the first one, this can lead to something similar to your problem.

Categories