Skip to content
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

AttributeError: module 'onnx' has no attribute 'load' #28835

Closed
ezyang opened this issue Oct 29, 2019 · 7 comments
Closed

AttributeError: module 'onnx' has no attribute 'load' #28835

ezyang opened this issue Oct 29, 2019 · 7 comments
Labels
module: onnx Related to torch.onnx triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@ezyang
Copy link
Contributor

ezyang commented Oct 29, 2019

When I run test/onnx/test_utility_funs.py locally I get an error:

======================================================================
ERROR: test_strip_doc_string (__main__.TestUtilityFuns)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/onnx/test_utility_funs.py", line 226, in test_strip_doc_string
    self.assertTrue(is_model_stripped(io.BytesIO()))
  File "test/onnx/test_utility_funs.py", line 220, in is_model_stripped
    model = onnx.load(io.BytesIO(f.getvalue()))
AttributeError: module 'onnx' has no attribute 'load'

======================================================================
ERROR: test_strip_doc_string (__main__.TestUtilityFuns_opset10)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test/onnx/test_utility_funs.py", line 226, in test_strip_doc_string
    self.assertTrue(is_model_stripped(io.BytesIO()))
  File "test/onnx/test_utility_funs.py", line 220, in is_model_stripped
    model = onnx.load(io.BytesIO(f.getvalue()))
AttributeError: module 'onnx' has no attribute 'load'

----------------------------------------------------------------------

I can fix it by running pip install onnx. But the error message here is unexpected; if onnx wasn't installed, why did I get an error that it imported successfully and just doesn't have any attributes?

When I print onnx.__file__ I get None. My best guess is that we are accidentally picking up test/onnx folder as a namespace.

cc @houseroad @spandantiwari @lara-hdr @BowenBao @neginraoof

@ifedan ifedan added module: onnx Related to torch.onnx triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 29, 2019
@spandantiwari
Copy link

Just add some context (i.e., not exactly the answer to the question) - to the best of my knowledge, onnx is not installed as a dependency of PyTorch. It is only a test dependency.

@nolifenameless
Copy link
Contributor

nolifenameless commented Jun 2, 2020

This error here is caused by the module 'onnx'(before pip install onnx) which can avoid python import inspection,

I try to recur this situation but still got nothing. @ezyang would you like provide info about this situation? that would be very helpful.

IMO, wrap this case "test_strip_doc_string" in some context which scenario should run(Ex. method _skipper in file test/onnx/test_pytorch_common.py ) may solve this problem

@ezyang
Copy link
Contributor Author

ezyang commented Jun 2, 2020

This issue is pretty old so I'm happy to just assume it's been silently fixed since then.

@ezyang ezyang closed this as completed Jun 2, 2020
@Manoj-Borkar
Copy link

I am still getting the same error with onnx.load. Is it resolved?? If yes, could someone pls point me in the right direction

@saadnajib
Copy link

I had the same problem fixed it in a way that, I had my python file name "onnx.py" same name as the library I was importing, changing the name of my python file from "onnx.py" to "converting_to_onnx.py" solved the issue.

@dron-dronych
Copy link

dron-dronych commented Dec 14, 2022

for those curious on why this happens is because when you run a py file named "onnx.py" it first adds that directory to your python search path. It then searches for a name to import (import onnx) and the first one it encounters is your directory with YOUR onnx.py. This behavior should be avoided as it adds unexpected behavior to the python import mechanism.

@kingydw
Copy link

kingydw commented Oct 19, 2023

I had the same problem fixed it in a way that, I had my python file name "onnx.py" same name as the library I was importing, changing the name of my python file from "onnx.py" to "converting_to_onnx.py" solved the issue.

I jumped into this trap too...😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: onnx Related to torch.onnx triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

8 participants