Hello Python community! There are a lot of resources online targeted at beginners that want to learn Python but very rarely do you see articles talking about moving to Python when you already have tons of experience in other languages like Ruby, and especially, many years of Perl experience and is interested in moving to Python.
I’m not looking for information on how to program in Python, that’s really easy to find and most of the learning curve will be learning about the standard libraries and overcoming the years of muscle memory from other languages. I’m looking for information on the following topics:
- What’s the recommended project structure for a library or a program that’ll be distributed via PyPI?
- What are the general best practices to follow when writing “clean Python code”?
- What’s the most commonly followed style guide for the language?
- How does import work internally and how does it perform its path lookup for local files (specifically for importing modules internal to a project)?
- How to properly set up pyenv for a project? (This one is tricky for me because the Python community loves pyenv and I’m used to having packages globally installed in Ruby and Perl)
IMHO programs don’t belong on pypi only libraries. Its a waste of namespace.
Explicit is better than implicit, read the zen of python(its short). Don’t be too magical. Don’t reach for a class if you have no state. Watch some jackdied talks from old pycons. You don’t need custom exceptions the stdlib has plenty. Also if its not documented don’t use it. Don’t use star imports.
Black just use it don’t fight it, don’t waste brain space with formatting rules. “You can have any formatter you want as long as its black”. Use default black settings. People who don’t are heretics :P
sys.path or PYTHONPATH is how imports are resolved. Much like shells looking for binaries. IMHO you should never adjust either. The current directory is always added to sys.path. but beyond that directories are packages. Use
__init__.py
when you want to provide a module for what happens when you import a package.This is how you should create your venv. After that activate it and use pip. You should setup a requirements.txt for your project, then you can use pip -r to configure your env. Global install of third-party libraries is always a bad idea in practice. Version constraints make that almost impossible these days.