Installing Python the Right Way

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.

Setting up your machine is an endeavor of patience, so I want to give you a way to still run your Python scripts without having to configure your computer to do so and that is to use Replit.

  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 that you can run in your browser. I like Replit, because it’s free and doesn’t have annoying ads. Plus if you muck up a project, it’s easy to start over again and you don’t have to manage your dependiences like you do if you do it all on your machine:

  2. OR Install Python:

    • 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 will need to download Xcode’s Command Line Utility, see this great guide on how to do it: Mac Install Guide

        • You can also just follow the instuctions on Python (python.org) or use Homebrew (https://brew.sh) to manage your Python versions and download Python and pip (short for “Pip Installs Packages”, which is 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)"
        

Once you have Homebrew installed, configure our environment:

  1. Configure Python environment

    • 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
      
  2. 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
    
  3. 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.
plaintext $ 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.

  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 using Nano and name it, hello_world.py 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. The code print("Hello, world!") will output “Hello, world!” once it is ran.

  • 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.

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!