altair-viz / vega_datasets

A Python package for online & offline access to vega datasets
MIT License
173 stars 57 forks source link

problem: import vega_datasets.data as ... #9

Closed ijlyttle closed 6 years ago

ijlyttle commented 6 years ago

Please forgive me as I am a Python "newbie" and may be asking an ignorant question.

I would like to be able to use the form:

import vega_datasets.data as data

instead of

from vega_datasets import data

My motivation is that I can use something analogous to the first form when using the import() function in the R reticulate package.

If I try this (in Python):

from vega_datasets import data
dir(data)

I get (as I expect):

['7zip',
 'airports',
 'anscombe',
 'barley',
 ...
 'zipcodes']

However, if I try this:

import vega_datasets.data as data2
dir(data2)

I get:

['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

whereas I am hoping to replicate the first behavior.

By contrast, this works as I expect:

import scipy.stats as stats
dir(stats)

Question: could it be possible for import vega_datasets.data as data to work like from vega_datasets import data?

Thanks!

jakevdp commented 6 years ago

Hi – the two import statements are not equivalent. from vega_datasets import data will import the data object from the vega_datasets module. On the other hand, import vega_datasets.data as data imports the submodule named vega_datasets.data, rather than the data object that is within the vega_datasets module.

Because the data object used by vega_datasets is not a module, there's no way that we can make import vega_datasets.data as data do what you want it to do. It is only possible to import a submodule this way, not an object from a module.

The reason it works for scipy.stats is because scipy.stats is a module object, not an object defined within the scipy namespace.

I hope that clears things up!

jakevdp commented 6 years ago

I've pushed a fix in #10 that will alleviate this confusion. Now doing import vega_datasets.data as data will raise an ImportError.

ijlyttle commented 6 years ago

Thanks! Reading your explanation, I should have used the word "hope" instead of "expect" ;)

realkrantz commented 5 years ago

When I run

import altair as alt
from vega_datasets import data

I am getting the error:

ModuleNotFoundError Traceback (most recent call last)

in () 1 import altair as alt ----> 2 from vega_datasets import data 3 4 iris = data.iris() 5 ModuleNotFoundError: No module named 'vega_datasets'

Any thoughts why?

ijlyttle commented 5 years ago

It almost looks like the package vega_datasets is not installed.

realkrantz commented 5 years ago

Thanks, @ijlyttle. How do I install the package vega_datasets?

ijlyttle commented 5 years ago

No problem, try this: https://github.com/altair-viz/vega_datasets#installation

realkrantz commented 5 years ago

Solved. Thanks, @ijlyttle.