"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?

3 comments

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.

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?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0

Yeah, that would explain it.  Right now my layout looks like

  • Oranges/
    • .idea/
    • doc/
    • scripts/
    • assets/
    • oranges/
      • __init__.py
      • foo.py
      • ...

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?

0

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.

Yeah, that would explain it.  Right now my layout looks like

  • Oranges/* .idea/

  • doc/

  • scripts/

  • assets/

  • oranges/* __init__.py

  • foo.py

  • ...

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?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0

Please sign in to leave a comment.