Installing Python the Right Way

Table of contents

Introduction

Hello there! Are you ready to dive into the wonderful world of Python programming? Great! Python is a fantastic language that's known for its simplicity and readability. In this guide, we'll cover what you should know before you start and set up your system so you learn Python quickly and easily.

  1. Install Python - python.org:

    • First things first, let's install Python! You can download it from the official website (python.org) and follow the installation instructions.

      • Follow the installation instructions there if you have a Windows machine

      • If you have a Mac, you can follow the instuctions on Python (python.org) or use Homebrew (https://brew.sh) to manage you python versions and download python and pip (Python’s package manager). I use Homebrew to manage my Python versions.

      • The command for installing Homebrew (you can find this on their website as well

          /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
        
    • brew install pyenvcommand in the Terminal window to install PyEnv utility on your Mac. You can switch between different Python versions with the aid of this PyEnv utility.

        $ brew install pyenv
      
    • pyenv intall version command to install Python 3 version you want to use on your Mac device after PyEnv is set up. (Here, 3.12.0 is the version number that Python currently has at the time of this tutorial. You can replace the version number with the one your need.)

        $ pyenv install 3.12.0
      
    • To see the version of python that you have installed, run the following. if the asterisk is in front of system, and you want your system to run the new version, you have to run pyenv global yourVersion to set the global version. The output should have the asterisk in front of the version:

    $ pyenv versions
    * system (set by /Users/shani/.pyenv/version)
      3.12.0

    $ pyenv global 3.12.0
    $ pyenv versions
      system (set by /Users/shani/.pyenv/version)
    * 3.12.0
  • Once you run the command to set your global version, you need to restart your terminal (just close it and open a new window) and type in python command to open the Python REPL and it should use the version you set globally.
    $ python
    Python 3.12.0 (main, Nov 22 2023, 12:40:42) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  1. Choose a Code Editor:

    • You'll need a place to write your Python code. Many people prefer using code editors like Visual Studio Code, PyCharm, or even the built-in IDLE. Pick the one that feels comfortable to you.

  2. Command Line & Git:

    Learn a bit of this, because you will be in command line a lot. I think the best resource is Codecademy’s courses, because they are free and very thorough.

    Intro to the Command Line | Codecademy

    Learn the Command Line: Viewing and Changing the File System | Codecademy

    Learn the Command Line: Redirecting Input and Output | Codecademy

    Classic SysAdmin: Understanding Linux File Permissions - Linux Foundation

    Learn Git: Introduction | Codecademy

  3. Setup you Project Folder Setup:

    • Create a folder:
    mkdir Python_Dojo
  • Go into the folder:
    cd Python_Dojo
  • Create a virtual environment inside your new folder. The venv command also creates a folder with the same name
    Python_Dojo % python3 -m venv venv
  • initialize Git

    • Create a file .gitignore, and
        Python_Dojo % git init
        Initialized empty Git repository in /Users/shanirivers/Documents/Python_Projects/FiringRange/.git
  • Add the /venv directory so that git will not track it. I created the .gitignore file and opened in all in Terminal. Edit the file and save it. The reason you don’t want to track this directory is that it will contain all the libraries and all their files as well. It will bloat your repository.
         Python_Dojo % nano .gitignore 
                                        # add this directory to it: /venv
  • Commit changes:
        Python_Dojo % git status
        On branch main

        No commits yet

        Untracked files:
          (use "git add <file>..." to include in what will be committed)
            .gitignore

        nothing added to commit but untracked files present (use "git add" to track)
        Python_Dojo % git add . 
        Python_Dojo ****% git commit -m "Initial commit. Add .gitignore to ignore virtual environment directory."
        [main (root-commit) 4cc932d] Initial commit. Add .gitignore to ignore virtual environment directory.
         1 file changed, 2 insertions(+)
         create mode 100644 .gitignore
  • Create file and add the Python code to it.

      Python_Dojo % nano hello_world.py
    
      #!/user/bin/env python3 
    
      print("Hello, world!")
    

    Shebang #!/user/bin/env python3 - add it to the file, hello_world.py

    Shebang tells the computer where to find the exact Python interpreter to use, in this case, I want to use python3.

  • Activate virtual environment and add Pylint into it. Then test the file you created and it should analyze it and return what is wrong with it.

      Python_Dojo ****% source venv/bin/activate
      (venv) Python_Dojo % pip install pylint
    
      (venv) Python_Dojo % pylint hello_world.py 
      ************* Module hello_world
      hello_world.py:1:0: C0114: Missing module docstring (missing-module-docstring)
    
      -----------------------------------
      Your code has been rated at 0.00/10
    

    Run the code to see if it outputs “Hello, world!”.

      (venv) Python_Dojo % python3 hello_world.py 
      Hello, world!
    
  • Deactivate your virtual environment, by typing in deactivate into the command line. And run git to make sure that the only file that needs to be committed is the one you created.

  1. Use an online IDE, like Replit. If you don’t want to do all the installation or setup stuff above, feel free to use a online IDE. I like Replit, it’s free and doesn’t have annoying ads:

    Replit: The software creation platform. IDE, AI, and Deployments

And there you have it, your virtual environment is set up and ready to use for your project and you will not have to worry about downloading any Python libraries that might duplicate or interfer with your native Python on your computer!