Python Forum
Help wanted with python imports
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help wanted with python imports
#1
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 (underscores to signify tabs...):

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):
from unittest import TestCase

class Test(TestCase):
    def test_contains(self):
        from src import utils
        line = ["one", " ", "class", "//"]
        self.assertTrue(utils.contains(line, "one"))
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.
Reply
#2
Quote: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.

You should have seen it before revised by Brett Cannon. Bottom line ... it works and I don't think anyone wants to tackle a complete rewrite. If interested in how it actually works, see: https://www.youtube.com/watch?v=Nsg886UOahw
Reply
#3
And btw - bad idea to import from within a function. The import will occur then every time the function is called, causing a dismal drain on your performance.
Reply
#4
(Apr-07-2021, 03:08 PM)petros21 Wrote: 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?
It can be a confusing at start before understand how it works.
Here are some post i done before with full examples,
try to look at it or better run some to understand what going on.
Full example of making a package
Simpler version
Another package
In tutorial goes trough a lot Packaging/Modules--Wheel--pip--setup.py--Freeze
There is a Repo later in post Packaging Tutorial which is easy to clone an run.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,543 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 1,912 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,544 Jun-27-2021, 08:30 PM
Last Post: Hpao
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 3,765 Mar-17-2021, 11:57 AM
Last Post: cff
  threading across imports Nickd12 2 2,104 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  refreshing imports seandepagnier 4 2,673 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 2,372 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  absolute imports between folders mikisDW 0 1,505 Aug-05-2020, 12:26 PM
Last Post: mikisDW
  Understanding the concept ( Modules , imports ) erfanakbari1 1 2,137 Nov-25-2019, 01:59 AM
Last Post: Larz60+
  How Do I Get the IDE to Find My Imports petec 18 9,037 Feb-24-2019, 02:50 AM
Last Post: petec

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020