Skip to main content

Python Project

·221 words·2 mins
Posts Essential 101 python py-venv
Table of Contents

Here is how I start a Python project for myself.

Creating Template for Python Project
#

  1. Create a Python project folder.
  2. Create virtual environment for project.
  3. Create the base file.
~ $ mkdir webping_project
~ $ cd webping_project
~/webping_project $ 

~/webping_project $ python3.11 -m venv .venv
~/webping_project $ source .venv/bin/activate
~/webping_project $ 

~/webping_project $ pip install requests
~/webping_project $ pip freeze > requirements.txt
~/webping_project $ 

~/webping_project $ vim main.py 
~/webping_project $ 

Executing Python Project
#

  1. Link the project file.
  2. Execute the project.
~/webping_project $ ln -s main.py __main__.py
~/webping_project $ ls -l
total 8.0K
lrwxrwxrwx 1 xx xx    7 Aug 24 16:57 __main__.py -> main.py*
-rwxr-xr-x 1 xx xx 2.9K Aug 24 16:54 main.py*
-rw-r--r-- 1 xx xx  154 Aug 24 16:56 requirements.txt

~/webping_project $ 
~/webping_project $ cd ..
~ $ 
~ $ python webping_project
usage: webping_project [-h] [-v] url [url ...]
webping_project: error: the following arguments are required: url

Here is how to execute the project in a more concise and elegant way. It makes it easier for others to use a project.

~ $ deactivate
~ $
~ $ python webping_project -v https://www.google.com
 * https://www.google.com        [200] Google
 * https://www.google.com        [200] Google
 * https://www.google.com        [200] Google
 * https://www.google.com        [200] Google
^C [*] DONE

Of course, the old way to run a project can be:

~ $ python webping_project/main.py -v https://www.google.com

Related

Use of f-string format() in Python
·242 words·2 mins
Posts 101 python
Using f-string in Python.
Chained Comparison
·132 words·1 min
Posts 101 python
Chained comparison in Python.
Insecurity in HTTP Headers
·2195 words·11 mins
Posts Essential async cli http python
Based on essential security, here is how to protect users by securing HTTP headers for a website.