Hacker News new | past | comments | ask | show | jobs | submit login

Years ago, I thought I'd try learning Python, I'd heard it was supposed to be easy, good for beginners and everything. I read one of those beginner Python type books and followed along with a roguelike tutorial. Everything was going pretty alright, until I started trying to split everything into different files and use imports.

I ended up just giving up. I read programming in lua, and rewrote my entire project in lua and actually finished it.

Some day I'd like to go back and maybe learn Python but I really didn't enjoy my experience with it. I even found C headers easier to figure out than Python imports.




Strange, Python’s import is not very difficult to use.


It's on of those things people using Python for so long forget about: Some people try to run individual files and cd around the place. I never do that anymore. I have a test suite and breakpoints and that's it. But before you've learned those tools it feels natural to "run that file there" and then say "oh hey why doesn't it work any more?"


It has some wildly frustratingly unintuitive behaviours in precisely the wrong place for beginners: in between having everything in a single script and building a proper package, especially when you are invoking your script with 'python script.py' as opposed to say 'python -m scripts.script'.


Yeah. Start writing a program in 'myprogram.py' as things grow do the right thing and split a function out to its own file and import it. It doesn't work. Suddenly you need to learn a whole bunch about python modules and the import system and scripts vs modules, and some of the questions you have just literally have no good answer.


Import by module:

    ⏵ ls
    module.py  script.py

    ⏵ cat module.py 
    def foo2():
        print('foo2 called.')
        
    ⏵ cat script.py 
    import module

    def foo1():
        print('foo1 called.')

    foo1()
    module.foo2()

    ⏵ python3 script.py
    foo1 called.
    foo2 called.

Import by name:

    ⏵ cat script.py 
    from module import foo2

    def foo1():
        print('foo1 called.')

    foo1()
    foo2()

    ⏵ python3 script.py
    foo1 called.
    foo2 called.


I moved module.py into a modules folder to clean things up and now I get:

"ImportError: attempted relative import with no known parent package"

Looks like I'm back to having to learn a bunch of stuff about scripts and modules again?


>python3 script.py

I was using Python 2 at the time. Python 3 was still relatively new. Not sure how much difference it makes for your example, but the import systems are different between 2 and 3.

https://nerdparadise.com/programming/python/import2vs3


It was probably something I did. The original tutorial I followed had everything in one file and didn't get into anything about imports. I started splitting everything up arbitrarily and started tossing imports into the files that complained about missing dependencies and ended up getting overwhelmed because nothing worked.

I'm sure if I'd taken the time to try and fix it I eventually could have and at this point i've had more experience with a bunch of different languages, so I'm sure it's not as bad as I remember.

I imagine it's one of those cases where if i were to go back and laugh about how stupid I was, but ya know, those first impressions.


No, your impression was right. Reading this blog post made me realize how little I know about the python import system (and I use python daily), and at the same time how little I want to learn it. It is completely unintuitive and probably one of the worst aspects of otherwise beautiful and useful language. Fortunately, sys.path hack works reliably - one can just add that one line and imports work as expected.


There's only about three things to learn at first.

If you know how a path and relative path work from the shell, there is only one thing: touch __init__.py.

These are on the import doc page and beginners tutorial.

https://docs.python.org/3/tutorial/modules.html#packages




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: