Python/Starting a Python Project.md
... ...
@@ -0,0 +1,177 @@
1
+# Starting a Python Project. This guide documents the steps I take… | by Lorenzo Uriel | Nov, 2024 | Medium
2
+
3
+https://medium.com/@lorenzouriel/starting-a-python-project-a-step-by-step-guide-0ce3b220b9bf
4
+
5
+![](https://miro.medium.com/v2/resize:fit:720/format:webp/1*VTS9D-TxqsizGo7dFSnLhw.png)
6
+
7
+This guide documents the steps I take to set up the foundation of any Python project, with a focus on environment setup, version control, and project structure.
8
+
9
+1\. Git & GitHub Repository
10
+---------------------------
11
+
12
+If your project doesn’t have a local and a remote repository, you could be in danger, Git and GitHub is essential for tracking changes and backing up your code.
13
+
14
+**Steps:**
15
+----------
16
+
17
+* Create a **local repository** in your project directory:
18
+
19
+```
20
+git init
21
+```
22
+
23
+
24
+* Create a **remote repository** on GitHub.
25
+* Link the remote repository to your local one and send your first commit:
26
+
27
+```
28
+echo "# python-requests-site" >> README.md
29
+git init
30
+git add README.md
31
+git commit -m "first commit"
32
+git branch -M main
33
+git remote add origin https://github.com/lorenzouriel/python-requests-site.git
34
+git push -u origin main
35
+```
36
+
37
+
38
+A project without version control could be at risk if anything goes wrong with your code
39
+
40
+2\. Virtual Environment
41
+-----------------------
42
+
43
+Using a virtual environment ensures that your project dependencies do not interfere with other Python projects on your system. It isolates your environment and keeps everything clean.
44
+
45
+**Steps:**
46
+----------
47
+
48
+* Create a virtual environment:
49
+
50
+```
51
+python -m venv venv
52
+```
53
+
54
+
55
+* Activate the virtual environment:
56
+
57
+```
58
+# Windows
59
+.\venv\Scripts\activate
60
+# macOS/Linux
61
+source .venv/bin/activate
62
+```
63
+
64
+
65
+Once activated, any Python packages you install will go to the .venv folder and not affect other projects.
66
+
67
+3\. Use Pyenv
68
+-------------
69
+
70
+Pyenv allows you to manage multiple Python versions on your system.
71
+
72
+It’s useful if you need to test your project with different Python versions or if you want to keep the Python versions for your projects isolated.
73
+
74
+Steps:
75
+------
76
+
77
+* Install Pyenv on your system by following the [installation guide](https://github.com/pyenv/pyenv).
78
+* Install a specific version of Python:
79
+
80
+```
81
+pyenv install 3.12.0
82
+```
83
+
84
+
85
+* Set the local Python version for your project
86
+
87
+```
88
+pyenv local 3.12.0
89
+```
90
+
91
+
92
+A file called _.python-version_ will be created in your project tree.
93
+
94
+This ensures that your project will use the specified Python version, no matter what the global system version is.
95
+
96
+4\. Use Poetry
97
+--------------
98
+
99
+Poetry is a dependency manager and build tool for Python projects.
100
+
101
+It simplifies dependency management and packaging, making it easier to maintain and share Python projects.
102
+
103
+**Steps:**
104
+----------
105
+
106
+* Install Poetry by following the [installation guide](https://python-poetry.org/).
107
+* Once installed, initialize Poetry in your project:
108
+
109
+```
110
+poetry init
111
+```
112
+
113
+
114
+* Poetry will guide you through creating a _pyproject.toml_ file, where your dependencies will be listed.
115
+* To install dependencies, run:
116
+
117
+```
118
+poetry install
119
+```
120
+
121
+
122
+* Poetry also allows you to create a virtual environment and set the python version:
123
+
124
+```
125
+poetry shell # Create and log into a shell within the virtual environment
126
+poetry env use 3.12.0 # Sets the version of Python to be used by virtual project environment
127
+```
128
+
129
+
130
+The main point is dependency resolution in a clean and efficient way.
131
+
132
+Here is a example of a _pyproject.toml_ file with the lib _requests_ installed:
133
+
134
+```
135
+[tool.poetry]
136
+name = "python-requests-site"
137
+version = "0.1.0"
138
+description = ""
139
+authors = ["Lorenzo Uriel <your-email@gmail.com>"]
140
+readme = "README.md"
141
+
142
+[tool.poetry.dependencies]
143
+python = "^3.12"
144
+requests = "^2.32.3"
145
+
146
+[build-system]
147
+requires = ["poetry-core"]
148
+build-backend = "poetry.core.masonry.api"
149
+```
150
+
151
+
152
+5\. Create a .gitignore and a .env
153
+----------------------------------
154
+
155
+**_.gitignore_**: This file tells Git which files and directories should not be tracked by version control. This helps prevent sensitive or unnecessary files from being pushed to GitHub.
156
+
157
+**_.env_**: This file contains environment variables such as API keys, database URLs and other configuration values. It’s important to keep this file private, so it should never be committed to the repository.
158
+
159
+**Steps:**
160
+----------
161
+
162
+* In the case of .gitignore I always use the toptal model, you can [check here](https://www.toptal.com/developers/gitignore/api/python).
163
+* Just create the files and add the values:
164
+
165
+```
166
+# Windows
167
+New-Item .gitignore
168
+New-Item .env
169
+# macOS/Linux
170
+touch .gitignore
171
+touch .env
172
+```
173
+
174
+
175
+Here’s a great starting point for creating a Python project from scratch, there are several other approaches you can take to ensure the project is maintained according to best practices.
176
+
177
+I will create another article for this!
... ...
\ No newline at end of file