The Python Project Folder Setup

Photo by David Clode on Unsplash

The Python Project Folder Setup

  1. Create a folder:

     mkdir Python_Dojo
    
  2. Go into the folder:

     cd Python_Dojo
    
  3. 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
    
  4. Initialize Git

    • Create a file .gitignore, and
    Python_Dojo % git init
    Initialized empty Git repository in /Users/shanirivers/Documents/Python_Projects/.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: /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
  1. 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.

  2. Activate virtual environment and add Pylint into it. Pylint is a static code analyzer. It analyzes your code without running it. It checks for errors, enforces coding standard and what could be refactored. 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. And run git to make sure that the only file that needs to be committed is the one you created.

     (venv) Python_Dojo % deactivate
     Python_Dojo % git status
     On branch main
     Untracked files:
       (use "git add <file>..." to include in what will be committed)
         hello_world.py