Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Help wanted with python imports

Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May i start with saying that the import-module-package system of python is the most abominable feature that I have ever encountered in any language ever.
With that out of the way, i have a project in pycharm with the following structure:



Also in my src folder i have a .bat file named py_run.bat that allows me to run the program from anywhere like this:

in order for my imports to work I am doing them like this (for example inside
from Extension import Extension (file has a class named Extension)
and this way the project runs from pycharm and from cmd too

But i made pycharm create a test for me (

When i run that i get an error on all my imports that are like that: "from Extension import Extension"
this error: ModuleNotFoundError: No module named 'Extension'

if I change the import to "src.Extension import Extension"
the test runs fine, the program from pycharm runs fine but from the cmd it says
ModuleNotFoundError: No module named 'src'

What is wrong with this language? How can it be so stupid and unintuitive for something so simple to work?
How am I supposed to structure my imports?
Thanks in advance.
Saloon Keeper
Posts: 23419
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know what you mean. I've struggled with it myself.

I think at the heart of your problem is  that the command-line Python is using a different PYTHONPATH.

Beyond that, it's hard to help, since I still fumble around a lot myself. But it sounds like maybe it's PyCharm that needs adjusting.

I'm not familiar with the product, but a good parallel might be Eclipse and Java. In Eclipse, you can mark one or more folders in a project to be source folders and that facilitates the ability to run tests while keeping the actual tests separate from the production code. You might want to see if PyCharm does something similar.

If you haven't already, try PyCharm''s "New Project" wiards and see if any of them generate something like what you want. That will give you a model of what you need.
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
    Bookmark Topic Watch Topic
  • New Topic