{"id":1568,"date":"2026-03-29T16:57:20","date_gmt":"2026-03-29T16:57:20","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=1568"},"modified":"2026-03-29T17:05:41","modified_gmt":"2026-03-29T17:05:41","slug":"how-to-create-a-python-project-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/how-to-create-a-python-project-step-by-step-guide\/","title":{"rendered":"How to Create a Python Project: Step by Step Guide"},"content":{"rendered":"\n<p>To create a simple Python project with dependencies, you need to set up a virtual environment, define your dependencies in a&nbsp;<code>requirements.txt<\/code>&nbsp;file, and write your code in&nbsp;<code>main.py<\/code>. This approach ensures project dependencies are isolated and easily installable on any system.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Project Setup Steps<\/h3>\n\n\n\n<p><strong>1. Create a Project Directory<\/strong><\/p>\n\n\n\n<p>Open your terminal or command prompt and create a new directory for your project, then navigate into it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir simple-python-project\ncd simple-python-project<\/code><\/pre>\n\n\n\n<p><strong>2. Create and Activate a Virtual Environment<\/strong><\/p>\n\n\n\n<p>A virtual environment keeps your project&#8217;s dependencies separate from other Python projects.<\/p>\n\n\n\n<p>Create Virtual Environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m venv venv\n<\/code><\/pre>\n\n\n\n<p>Activate the environment:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># macOS\/Linux\nsource venv\/bin\/activate\n\n# Windows\n.\\venv\\Scripts\\activate\n\n<\/code><\/pre>\n\n\n\n<p>You should see&nbsp;<code>(venv)<\/code>&nbsp;at the beginning of your terminal prompt, indicating the environment is active.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(venv) D:\\python\\simple-python-project&gt; <\/code><\/pre>\n\n\n\n<p><strong>3. Create the&nbsp;<code>requirements.txt<\/code>&nbsp;file<\/strong><\/p>\n\n\n\n<p>This file lists the external libraries your project needs. For this example, we will use the&nbsp;<code>requests<\/code>&nbsp;library to make an HTTP request and python-dotenv to load configuration from <code>.env<\/code> i.e. the environment file.<br>Create a file named&nbsp;<code>requirements.txt<\/code>&nbsp;in your project root and add the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>requests\npython-dotenv<\/code><\/pre>\n\n\n\n<p><strong>4. Install Dependencies<\/strong><\/p>\n\n\n\n<p>With your virtual environment activated, install the packages listed in&nbsp;<code>requirements.txt<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(venv) D:\\python\\simple-python-project&gt; pip install -r requirements.txt\n<\/code><\/pre>\n\n\n\n<p><strong>5. Create&nbsp;<code>main.py<\/code><\/strong><\/p>\n\n\n\n<p>Create the main application file named&nbsp;<code>main.py<\/code>&nbsp;and add some Python code that uses the installed dependency:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport requests\nimport sys\nfrom dotenv import load_dotenv\n\n# 1. Load variables from the .env file\nload_dotenv()\n\ndef main():\n    \n    # 2. Get the API URL from environment variables\n    api_url = os.getenv(\"API_URL\")\n\n    print(f\"Attempting to fetch data from: {api_url}\")\n\n    if not api_url:\n        print(\"Error: API_URL not found in .env file.\")\n        return\n    \n    try:\n        response = requests.get(api_url)\n        response.raise_for_status()\n\n        data = response.json()\n        \n        # GitHub events return a list; we check if it's not empty\n        if isinstance(data, list) and len(data) &gt; 0:\n            print(f\"Successfully fetched {len(data)} events.\")\n            # Note: Not all events have an 'action' key, using .get() for safety\n            action = data&#91;0].get('payload', {}).get('action', 'N\/A')\n            print(f\"First event payload snippet (action): {action}\")\n        else:\n            print(\"Fetched data, but it wasn't in the expected list format.\")\n\n    except requests.exceptions.RequestException as e:\n        print(f\"An error occurred: {e}\")\n        sys.exit(1)\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p>This script safely reach out to the internet, grab specific data, and show it to you.<\/p>\n\n\n\n<p><strong>6. Create Environment Variables<\/strong><\/p>\n\n\n\n<p>Create a&nbsp;<code>.env<\/code>&nbsp;file. This file is used to store secrets like API keys, API endpoints etc. We\u2019ll add the API URL here to show how to load external config. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>API_URL=https:\/\/api.github.com\/events<\/code><\/pre>\n\n\n\n<p><strong>7. Run the Project<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(venv) D:\\python\\simple-python-project&gt; python main.py\n<\/code><\/pre>\n\n\n\n<p>You will see output confirming that the&nbsp;<code>requests<\/code>&nbsp;library successfully made the web request.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Output\nAttempting to fetch data from: https:\/\/api.github.com\/events\nSuccessfully fetched 30 events.\nFirst event payload snippet (action): N\/A<\/code><\/pre>\n\n\n\n<p><strong>Important Note:<\/strong><\/p>\n\n\n\n<p>If the <code>.env<\/code> file contains secret API keys, we should add it in <code>.gitinore<\/code> file to prevent check-in.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.env<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Project Structure Summary<\/h3>\n\n\n\n<p>Your final project directory structure will look like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>simple-python-project\/\n\u251c\u2500\u2500 .venv\/               # Virtual environment\n\u251c\u2500\u2500 .env                 # API configuration &amp; secrets\n\u251c\u2500\u2500 .gitignore           # Files to exclude from Git\n\u251c\u2500\u2500 main.py              # Main execution logic\n\u251c\u2500\u2500 README.txt           # Project instructions\n\u2514\u2500\u2500 requirements.txt     # List of dependencies\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a simple Python project with dependencies, you need to set up a virtual environment, define your dependencies in a&nbsp;requirements.txt&nbsp;file, and write your code in&nbsp;main.py. This approach ensures project&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[292,304,305,306],"class_list":["post-1568","post","type-post","status-publish","format-standard","hentry","category-python","tag-learn-python","tag-python-project","tag-python-tutorial","tag-step-by-step-guide","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=1568"}],"version-history":[{"count":25,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1568\/revisions"}],"predecessor-version":[{"id":1594,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/1568\/revisions\/1594"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=1568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=1568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=1568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}