• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Help wanted with python imports

 
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
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:

CodeStats
    src
          __init__.py
          code_stats.py
          Extension.py
          utils.py
    tests
          __init__.py
          test_one.py

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:
py_run code_stats.py

in order for my imports to work I am doing them like this (for example inside utils.py):
from Extension import Extension (file Extension.py 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 (test_one.py):

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: 23409
159
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.
 
Acetylsalicylic acid is aspirin. This could be handy too:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic