DEV Community

Rob McBryde
Rob McBryde

Posted on

Embarking on a Django journey - Coding for the joy of it

Django setup on Mac with venv, PyCharm and Black.

As a software engineering manager entrenched in the world of people management and administrative tasks, the yearning to reconnect with the sheer joy of coding has led me to explore a new and exciting realm: the Django framework in Python. In this blog post, I'll share the motivations behind this creative coding endeavour and delve into the initial steps of setting up a development environment on my Mac.

A Creative Coding Escape

Amidst the hustle of managerial responsibilities, I found myself yearning for a coding outlet that could both rekindle the joy of programming and introduce me to unfamiliar territories. Django, a high-level web framework written in Python, presented itself as an intriguing choice. The decision to delve into a language I have limited professional experience with and a framework entirely unexplored felt perfect for me.

Setting the Stage: Python and Django on Mac

The first step on this journey involved setting up a development environment tailored for Python and Django on my Mac.

Pyenv

In my experience it is always advisable to install a version manager when working with programmgin languages where possible. Your future self will help you for it if you start to work on multiple projects in the future. In order to install Python 3 onto my Mac I used pyenv which I installed via Homebrew.

Python virtual environments

Consider working on multiple Django projects at once. Project A uses Django 3.2, but Project B uses Django 4.2. By default, Python and Django are installed globally on a computer: installing and reinstalling different versions every time you want to switch between project, quickly becomes a real pain.

Virtual environments allow you to create and manage separate environments for each Python project on the same computer. Within the Python community, the use of virtual environments seems non debatable. I have opted to use venv which comes installed as part of Python version 3.3, so fits my needs perfectly.

I am using the convention of creating a .venv directory alongside each of my Django projects.

mkdir helloworld
cd helloworld
python3 -m venv .venv
# active the virtual environment
source .venv/bin/activate
python3 -m pip install django~=4.2.0
Enter fullscreen mode Exit fullscreen mode

The above command to install Django in my virtual environment uses the comparison operator ~=, which installs the latest version of Django 4.2.x.

I didn't use pip install but rather python3 -m pip install? The former does work, but according to my reading around the Python community, it can cause some issues. Using the latter version of the comman ensures that the intended vesion of Python is in use, even if you have multiple versions of Python installed in your machine. Given that Macs often have Python 2.7 installed, it seems like a sensible approach to me to ensure the intended version of Python is being used.

Crafting Code in Comfort: PyCharm IDE

Choosing the right Integrated Development Environment (IDE) is crucial for a seamless coding experience. Enter PyCharm – my preferred IDE for this Django expedition. Its robust features, intelligent code completion, and Django support make it an invaluable companion for navigating the intricacies of web development. The comfort of PyCharm enhances the joy of coding, allowing me to focus on creativity rather than grappling with the tool. I'm a long term fan of JetBrains, so sticking with what I know.

Code Formatting Bliss with Black

No coding journey is complete without attention to code formatting. Enter "Black," the uncompromising code formatter for Python. Its opinionated approach streamlines the process, ensuring consistent and readable code. Integrating Black into the workflow not only adheres to best practices but also eliminates unnecessary debates over code styling.

python3 -m pip install black 
Enter fullscreen mode Exit fullscreen mode

As of PyCharm 2023.2, Black integration has been possible via Settings > Tools > Black. Simply select your Python interpreter as your (helloworld) virtual env. FYI I'm currently using Python 3.10 but will no doubt change soon enough (easily due to pyenv). I happen to like the Black formatter running 'On save'

Conclusion

That's my basic setup complete.

In the coming posts, I'll be documenting my learning experiences, pitfalls, and triumphs in the Django universe. While the primary aim is personal growth and enjoyment, I hope this documentation serves as a valuable resource for others venturing into Django development. Here's to coding for the sheer thrill of it!

Top comments (0)