[proxy] web.archive.org← back | site home | direct (HTTPS) ↗ | proxy home | ◑ dark◐ light
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

from x import y: y can only be a module, not any old attribute #99

Closed
kokes opened this issue Jan 12, 2017 · 4 comments
Closed

from x import y: y can only be a module, not any old attribute #99

kokes opened this issue Jan 12, 2017 · 4 comments

Comments

Copy link

kokes commented Jan 12, 2017

Importing certain classes or other objects from packages results in packages not found. E.g. os.path works just fine (from os import path), because build/src/os/path exists, but many packages (I came across math and collections) are implemented in a single Go file, so things like from math import pi or from collections import defaultdict will fail like this:

$ cat mth.py
from math import pi
print pi
$ ./build/bin/grumpc mth.py > mth.go
$ go run mth.go
mth.go:5:2: cannot find package "grumpy/lib/math/pi" in any of:
	/usr/local/Cellar/go/1.7/libexec/src/grumpy/lib/math/pi (from $GOROOT)
	/Users/ondrej/(...)/grumpy/build/src/grumpy/lib/math/pi (from $GOPATH)

Importing the package as a whole works just fine:

$ cat mth.py
import math
print math.pi
$ python2 mth.py
3.14159265359
$ ./build/bin/grumpc mth.py > mth.go
$ go run mth.go
3.141592653589793

Maybe I'm missing something. I'm running b730a44.

Copy link
Collaborator

trotterdylan commented Jan 12, 2017

Thanks for reporting this. This is a limitation of the current import system: you can only import whole modules not individual members from the module. The reason is for the statement from foo import bar, the compiler currently doesn't know whether bar is a module or a member, so it assumes it's a module. For the compiler to know better it would have to examine the GOPATH (or the PYTHONPATH I suppose) to make that determination.

I've added this to the missing features list.

trotterdylan changed the title from package import x Module attribute imports not supported: from package import x Jan 12, 2017
Copy link

toadzhou commented Jan 13, 2017

Can not use Python's own modules?
Can I use Golang's modules only? Python connection built-in json modules are not, but do not Python source inside the third-party libraries.

Copy link
Collaborator

trotterdylan commented Jan 13, 2017

Copy link
Contributor

S-YOU commented Jan 13, 2017

I think that is intentional because of Google style guide prohibit it according to this.

  # NOTE: Assume that the names being imported are all modules within a
  # package. E.g. "from a.b import c" is importing the module c from package
  # a.b, not some member of module b. We cannot distinguish between these
  # two cases at compile time and the Google style guide forbids the latter
  # so we support that use case only.

May be worth to mention somewhere in docs?

trotterdylan changed the title Module attribute imports not supported: from package import x from x import y: y can only be a module, not any old attribute Jan 13, 2017
davidc05 added a commit to davidc05/grumpy1 that referenced this issue Jul 20, 2020
Modules being imported are now detected at compile time. So it's
possible to determine whether "from x import y" is importing module y or
a member y of module x. This change uses this capability to choose
whether to import a module or a member.

This fixes google/grumpy#99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants