Python Forum
Help wanted with python imports - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help wanted with python imports (/thread-33226.html)



Help wanted with python imports - petros21 - Apr-07-2021

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.


RE: Help wanted with python imports - Larz60+ - Apr-07-2021

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


RE: Help wanted with python imports - jefsummers - Apr-07-2021

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.


RE: Help wanted with python imports - snippsat - Apr-07-2021

(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.