Python Basics: Setting up the environment

Python Basics: Setting up the environment

For more posts about Python, follow the link below👇👇👇

Exploring Python
Python is frequently utilized in creating websites and software, as well as for automating tasks, analyzing data, and visualizing information.

Before going anywhere further, we definitely have to prepare our environment. We need to install Python and a code editor called IDE (Integrated Development Environment).

I set up my environment on macOS, therefore I use Homebrew package manager to automate some steps. To install Homebrew, please check the following post.

Homebrew: a missing package manager
Learn how Homebrew simplifies software installation, manages dependencies, and enhances productivity.

To install Python (3.11 in our case), you have to issue brew install python3.

❯ brew install python3
==> Fetching python@3.11
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.11/manifests/3.11.4_1
Already downloaded: /Users/slysj/Library/Caches/Homebrew/downloads/c009c3cb6566d4e36121a40c85204870afab2b9c86263b1d38d3c672361c6cb1--python@3.11-3.11.4_1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.11/blobs/sha256:c97986e611c3c5e092be2ad70876fe99c7c7c23c8e7e63caaac6274512fbf657
Already downloaded: /Users/slysj/Library/Caches/Homebrew/downloads/fbff36e9b28bc995342f9a88fe64a107ac56db2daed6ae2f96d73ff132557f73--python@3.11--3.11.4_1.ventura.bottle.tar.gz
==> Installing python@3.11
==> Pouring python@3.11--3.11.4_1.ventura.bottle.tar.gz
==> /usr/local/Cellar/python@3.11/3.11.4_1/bin/python3.11 -m ensurepip
==> /usr/local/Cellar/python@3.11/3.11.4_1/bin/python3.11 -m pip install -v --no-deps --no-index --upgrade --isolated --target=/usr/local/lib/python
🍺  /usr/local/Cellar/python@3.11/3.11.4_1: 3,286 files, 61.2MB
==> Running `brew cleanup python@3.11`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

This command downloads Python, unpacks it, and installs as well as creates necessary symlinks. Once installation is finished, we can enter Python's interpreter.

❯ python3
Python 3.11.4 (main, Jun 20 2023, 16:59:59) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello")
Hello
>>>

As a next step, let's install Poetry. It is a dependency management and packaging tool for Python. It aims to simplify and streamline the process of managing project dependencies and creating Python packages. Poetry was created to address some of the shortcomings and complexities of other dependency management tools like pip and setuptools.

To install it, we issue brew install poetry.

❯ brew install poetry
==> Fetching poetry
==> Downloading https://ghcr.io/v2/homebrew/core/poetry/manifests/1.5.1
Already downloaded: /Users/slysj/Library/Caches/Homebrew/downloads/372ae6f64d6dc084243e75fbba1ff0ae68da124c1ce814ec3a352078e3f718f2--poetry-1.5.1.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/poetry/blobs/sha256:26cecd1a1828aa2a81a2c6adb5fd5bb91cb67ff195407521a9e37833041f1b89
Already downloaded: /Users/slysj/Library/Caches/Homebrew/downloads/c8a8476863931280d34ba2c0acf39291498c75d8f0295929de6bdc6a53c2d74c--poetry--1.5.1.ventura.bottle.tar.gz
==> Installing poetry
==> Pouring poetry--1.5.1.ventura.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/poetry/1.5.1: 2,265 files, 29.7MB
==> Running `brew cleanup poetry`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

Once installation is finished, we can run it.

❯ poetry
Poetry (version 1.4.2)

Usage:
  command [options] [arguments]

Options:
  -h, --help                 Display help for the given command. When no command is given display help for the list command.
  -q, --quiet                Do not output any message.
  -V, --version              Display this application version.
      --ansi                 Force ANSI output.
      --no-ansi              Disable ANSI output.
  -n, --no-interaction       Do not ask any interactive question.
      --no-plugins           Disables plugins.
      --no-cache             Disables Poetry source caches.
  -C, --directory=DIRECTORY  The working directory for the Poetry command (defaults to the current working directory).
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.

Available commands:
  about              Shows information about Poetry.
  add                Adds a new dependency to pyproject.toml.
...

Now we are able to write Python scripts, but writing them in a terminal using Vim is kind of cumbersome. Therefore, we will download and install IDE to make ourselves comfortable. My recommendation is to install PyCharm CE by JetBrains.

PyCharm: the Python IDE for Professional Developers by JetBrains
The Python & Django IDE with intelligent code completion, on-the-fly error checking, quick-fixes, and much more…

Let's open the website in the web browser and navigate to the download page. Instead of downloading the Professional version, scroll down a bit to download PyCharm Community Edition.

When the download is finished, double-click the downloaded file to open it.

Now drag and drop PyCharm CE into Applications to install it. Now you can open the Launchpad and look for the PyCharm CE icon and of course, run it. Upon the first run, the macOS system may ask you to confirm that application was downloaded from the Internet, so just open it. Now you should see the PyCharm welcome screen - and this is where the adventure begins.

Now, let's create a Sandbox project, so hit New Project. A new project configuration dialog will appear.

Rename location to sandbox, pick Poetry from the new environment drop-down list and choose Python 3.11 base interpreter. Then click Create.

Our project is created - it contains main.py with a sample script.

Hitting the green arrow in the upper right corner will execute the script.

Our Python script works! It has printed "Hi, PyCharm" to the console output!

Enjoy!

What’s next?

Subscribe to receive more posts like this directly to your email!

Ideas💡, questions❔❓ ? Feel free to start the discussion 🗣️🎙️!

Python Basics: Basic syntax
For more posts about Python, follow the link below👇👇👇 Exploring PythonPython is frequently utilized in creating websites and software, as well as for automating tasks, analyzing data, and visualizing information.slys.devJakub Slys In the previous post, we installed Python and configured IDE. Yo…