DEV Community

Discussion on: What is the most confusing thing to you in Python?

Collapse
 
ethnt profile image
Ethan Turkeltaub • Edited

Oh my god, the import/package system.

My senior thesis project was a Python project, and I was coming from the Ruby world. Dear lord, I never got used to the import system. The fact that you have to throw in something like this to import anything local astounded me:

import os
import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import foo.bar

Circular dependencies tripped me up quite a bit too, but I can probably put that down to lack of experience in Python. But man, coming from a world where you could just do this, it was a bit of a shock:

require 'foo/bar'
Collapse
 
tamas profile image
Tamás Szelei

In Python3 it's a lot saner (still not perfect though)

Collapse
 
grahamlyons profile image
Graham Lyons

The path insert is fairly horrible but I don't believe it's recommended.

Setting the PYTHONPATH environment variable can be useful in a local project (e.g. export PYTHONPATH=. in the root of your project), then using packages and absolute imports if you've got a lot of modules.

You can also import local modules just by using their name e.g. import my_module to import my_module.py from the same directory.

I find the Ruby require system a bit too close to PHP for my liking. I prefer that Python and Java abstract away the file system. However, using Bundler to manage your dependencies and put them onto the Ruby path at runtime is better than anything Python has.

Collapse
 
redgreenrepeat profile image
Andrew • Edited

how about having the __init__.py file in the foo/ ?