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
monkeytype/typing.py import error in PyCharm #67
Comments
This is weird... i can import from python console ok in the venv, just blows up when running cli.py in pycharm. |
Sounds like pycharm just isn’t using the venv. I don’t use pycharm so not sure how to make it use the venv, but that will be necessary to use it for monkeytype development. |
@carljm Maybe another contributer/watcher/user will have the answer? Pycharm is a very popular IDE, it seems likely that someone else will run into the same issue. In this case, it appears to be caused by monkeytype/typing.py stepping on the namespace for builtin typing, which is confusing pycharm. If i rename typing.py and all references to monkey_typing, the import error goes away. |
Sure, I can leave this open for awhile to see if another PyCharm user can help. Seems like PyCharm is either a) setting up |
Yep, it does seem like the sys.path is getting messed up: print(json.dumps(sys.path, indent=2)) I did a little digging, at it seems that this is default behavior for python: "projects/pathproblem" is added to sys.path by Python itself, not by PyCharm. See http://docs.python.org/2/library/sys.html#sys.path : "As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter." Because i was running /MonkeyType/monkeytype/cli.py as a script, it was adding the monkeytype directory to the path automatically. For anyone else who may run into this path conflict while developing/debugging, my solution was just to add a run.py file to the top level directory of the project:
|
Ah, makes sense! FWIW, when developing MonkeyType outside of PyCharm, it gets Thanks for digging and finding the workaround! |
@carljm can we just rename I wasted 3++ hours due to
inside monkeytype/typing.py importing itself. (though before that 3++ hours, everything was OK - even on pycharm / terminal. And then, some changes on monkeytype source-code caused havoc...) Also, according to https://stackoverflow.com/questions/6031584/importing-from-builtin-library-when-module-with-same-name-exists , it's better to have not-conflicting module names (though this seems to be a big problem on python-side: I never experienced this kind of problem in node-js imports / java imports because they have clear distinction on local / global imports...) |
Today modern Python no longer has any ambiguity between top-level and local-relative imports, so the only name that needs to not conflict is the top-level name, and our top-level name is "monkeytype", which does not conflict. The only way this can break is if something adds the wrong path to I'm sorry you lost some time debugging a path issue, but I don't think that a rename of |
Trying to set up dev environment per instructions in CONTRIBUTING.rst
Getting an import error:
Traceback (most recent call last):
File "/home/johnar/github/MonkeyType/monkeytype/cli.py", line 17, in
from typing import (
File "/home/johnar/github/MonkeyType/monkeytype/typing.py", line 10, in
from typing import (
ImportError: cannot import name 'Any'
In pycharm it actually shows that import _Any and _Union result in unresolved reference, as they're not defined in the typing module.
This is with stdlib typing 3.6.4:
typing 3.6.4
The text was updated successfully, but these errors were encountered: