"add import" action leaving off package?
I write some code, and pycharm correctly figures out I'm using something that hasn't been imported in the "foo" module, and I tell it to go ahead and add the import statement for me.
But whereas I expect it to be "from myproj.foo import Thing", it just says "from foo import Thing", leaving off the myproj package.
"myproj" is a subdirectory of the project's root directory.
Where have I gone wrong?
Please sign in to leave a comment.
Hello Kevin,
Is "myproj" marked as a source root in Settings | Project Structure? PyCharm
builds an import path relative to the closest content or source root above
the file to be imported.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Yeah, that would explain it. Right now my layout looks like
and I have "Oranges/oranges" set as a source root, which I guess is why it does "import foo" instead of "import Oranges.foo".
The simple fix seems like it'd be to set Oranges as the source root, but I don't want to do that, right? Because then all those non-source directories would get in there. I don't want to *exclude* all those, as I don't want them to be invisible.
I could introduce another level so it'd be Oranges/src/oranges/__init__.py, but that seems unnecessary. So is this a bug? Or pythonpath configuration feature?
Hello Kevin,
There's no harm at all in setting Oranges as the source root. The non-code
files in the sourcepath don't affect anything.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"