kristofer revidoval tento gist . Přejít na revizi
1 file changed, 1 insertion
why-pip.md
@@ -119,3 +119,4 @@ Data retrieved successfully: | |||
119 | 119 | ||
120 | 120 | **And you still need to to understand how to use [Python Virtual Environments](https://zcw.guru/kristofer/python3-venvs).** | |
121 | 121 | ||
122 | + | _And another thing, what the hell is [PyPi](https://pypi.org)??_ |
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 2 insertions, 1 deletion
why-pip.md
@@ -113,7 +113,8 @@ And now when we run `fetchjson.py` | |||
113 | 113 | ```bash | |
114 | 114 | (venv) foo@bar ~ % python3 fetchjson.py | |
115 | 115 | Data retrieved successfully: | |
116 | - | {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'} | |
116 | + | {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', | |
117 | + | 'body': 'quia et suscipit\nsuscipit recusandae consequuntur reprehenderit sunt rem eveniet architecto'} | |
117 | 118 | ``` | |
118 | 119 | ||
119 | 120 | **And you still need to to understand how to use [Python Virtual Environments](https://zcw.guru/kristofer/python3-venvs).** |
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 3 insertions, 4 deletions
why-pip.md
@@ -80,12 +80,11 @@ Our `python` environment doesn't have a copy of `requests` and can't find the co | |||
80 | 80 | ||
81 | 81 | _What is `requests` anyway? Well, check out [requests 2.32.3](https://pypi.org/project/requests/)_ | |
82 | 82 | ||
83 | - | So need to get a copy of this package. | |
84 | - | ||
83 | + | So we need to get a copy of this package. | |
85 | 84 | In a terminal, we can `pip install requests` and we see... | |
86 | 85 | ||
87 | 86 | ```bash | |
88 | - | (venv) kristofer@Atlantan ~ % pip install requests | |
87 | + | (venv) foo@bar ~ % pip install requests | |
89 | 88 | Collecting requests | |
90 | 89 | Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB) | |
91 | 90 | Collecting charset-normalizer<4,>=2 (from requests) | |
@@ -112,7 +111,7 @@ And now when we run `fetchjson.py` | |||
112 | 111 | ||
113 | 112 | ||
114 | 113 | ```bash | |
115 | - | (venv) kristofer@Atlantan ~ % python3 fetchjson.py | |
114 | + | (venv) foo@bar ~ % python3 fetchjson.py | |
116 | 115 | Data retrieved successfully: | |
117 | 116 | {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'} | |
118 | 117 | ``` |
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 1 insertion, 1 deletion
why-pip.md
@@ -117,5 +117,5 @@ Data retrieved successfully: | |||
117 | 117 | {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'} | |
118 | 118 | ``` | |
119 | 119 | ||
120 | - | **And you still need to to understand how to use Python Virtual Environments.** | |
120 | + | **And you still need to to understand how to use [Python Virtual Environments](https://zcw.guru/kristofer/python3-venvs).** | |
121 | 121 |
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 64 insertions
why-pip.md
@@ -54,4 +54,68 @@ if __name__ == "__main__": | |||
54 | 54 | ||
55 | 55 | but when you run this code... | |
56 | 56 | ||
57 | + | ```bash | |
58 | + | Traceback (most recent call last): | |
59 | + | File "<python-input-0>", line 1, in <module> | |
60 | + | import requests | |
61 | + | ModuleNotFoundError: No module named 'requests' | |
62 | + | Traceback (most recent call last): | |
63 | + | File "<python-input-0>", line 14, in <module> | |
64 | + | get_data() | |
65 | + | ~~~~~~~~^^ | |
66 | + | File "<python-input-0>", line 5, in get_data | |
67 | + | response = requests.get(url) | |
68 | + | ^^^^^^^^ | |
69 | + | NameError: name 'requests' is not defined | |
70 | + | ``` | |
71 | + | ||
72 | + | Hmm. | |
73 | + | ||
74 | + | ### PIP to the rescue. | |
75 | + | ||
76 | + | Notice the `import requests` line at the top of the code sample. | |
77 | + | ||
78 | + | That's the error. | |
79 | + | Our `python` environment doesn't have a copy of `requests` and can't find the code. | |
80 | + | ||
81 | + | _What is `requests` anyway? Well, check out [requests 2.32.3](https://pypi.org/project/requests/)_ | |
82 | + | ||
83 | + | So need to get a copy of this package. | |
84 | + | ||
85 | + | In a terminal, we can `pip install requests` and we see... | |
86 | + | ||
87 | + | ```bash | |
88 | + | (venv) kristofer@Atlantan ~ % pip install requests | |
89 | + | Collecting requests | |
90 | + | Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB) | |
91 | + | Collecting charset-normalizer<4,>=2 (from requests) | |
92 | + | Downloading charset_normalizer-3.4.0-cp313-cp313-macosx_11_0_arm64.whl.metadata (34 kB) | |
93 | + | Collecting idna<4,>=2.5 (from requests) | |
94 | + | Downloading idna-3.10-py3-none-any.whl.metadata (10 kB) | |
95 | + | Collecting urllib3<3,>=1.21.1 (from requests) | |
96 | + | Downloading urllib3-2.2.3-py3-none-any.whl.metadata (6.5 kB) | |
97 | + | Collecting certifi>=2017.4.17 (from requests) | |
98 | + | Downloading certifi-2024.8.30-py3-none-any.whl.metadata (2.2 kB) | |
99 | + | Downloading requests-2.32.3-py3-none-any.whl (64 kB) | |
100 | + | Downloading certifi-2024.8.30-py3-none-any.whl (167 kB) | |
101 | + | Downloading charset_normalizer-3.4.0-cp313-cp313-macosx_11_0_arm64.whl (119 kB) | |
102 | + | Downloading idna-3.10-py3-none-any.whl (70 kB) | |
103 | + | Downloading urllib3-2.2.3-py3-none-any.whl (126 kB) | |
104 | + | Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests | |
105 | + | Successfully installed certifi-2024.8.30 charset-normalizer-3.4.0 idna-3.10 requests-2.32.3 urllib3-2.2.3 | |
106 | + | ||
107 | + | [notice] A new release of pip is available: 24.2 -> 24.3.1 | |
108 | + | [notice] To update, run: pip install --upgrade pip | |
109 | + | ``` | |
110 | + | ||
111 | + | And now when we run `fetchjson.py` | |
112 | + | ||
113 | + | ||
114 | + | ```bash | |
115 | + | (venv) kristofer@Atlantan ~ % python3 fetchjson.py | |
116 | + | Data retrieved successfully: | |
117 | + | {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'} | |
118 | + | ``` | |
119 | + | ||
120 | + | **And you still need to to understand how to use Python Virtual Environments.** | |
57 | 121 |
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 37 insertions
why-pip.md
@@ -1,3 +1,39 @@ | |||
1 | + | ## Why PIP? | |
2 | + | ||
3 | + | "pip install" is crucial in the Python ecosystem for several reasons: | |
4 | + | ||
5 | + | ||
6 | + | Imagine you're building a web application: You might need various libraries to | |
7 | + | handle web frameworks, database connections, or even data visualization. | |
8 | + | While Python comes with a rich standard library, many powerful tools and packages are not included by default. | |
9 | + | Here's where "pip install" comes into play. | |
10 | + | ||
11 | + | ### Example: | |
12 | + | ||
13 | + | Suppose you want to build a web application using Flask, a popular web framework for Python. | |
14 | + | Flask isn't included in the Python standard library. To get Flask, you use "pip install": | |
15 | + | ||
16 | + | ```bash | |
17 | + | pip install Flask | |
18 | + | ``` | |
19 | + | ||
20 | + | Without "pip install," you would have to manually download Flask and manage its dependencies, | |
21 | + | which can be cumbersome and error-prone. "pip install" automates this process, ensuring you | |
22 | + | get the correct version of Flask and all its dependencies, making your development process | |
23 | + | smoother and more efficient. | |
24 | + | ||
25 | + | ||
26 | + | In essence, "pip install" allows you to easily extend Python's capabilities by accessing a | |
27 | + | vast repository of third-party packages (at PyPy), facilitating quicker development and | |
28 | + | ensuring that you can leverage the best tools available. | |
29 | + | ||
30 | + | ## So in Real Life? | |
31 | + | ||
32 | + | Here's a simple example of a Python program using the requests package to fetch data from a web API. | |
33 | + | This program will make a GET request to a sample API and print the response: | |
34 | + | ||
35 | + | ||
36 | + | Say `fetchjson.py` | |
1 | 37 | ||
2 | 38 | ```python | |
3 | 39 | import requests | |
@@ -16,5 +52,6 @@ if __name__ == "__main__": | |||
16 | 52 | get_data() | |
17 | 53 | ``` | |
18 | 54 | ||
55 | + | but when you run this code... | |
19 | 56 | ||
20 | 57 |
kristofer revidoval tento gist . Přejít na revizi
1 file changed, 20 insertions
why-pip.md(vytvořil soubor)
@@ -0,0 +1,20 @@ | |||
1 | + | ||
2 | + | ```python | |
3 | + | import requests | |
4 | + | ||
5 | + | def get_data(): | |
6 | + | url = "https://jsonplaceholder.typicode.com/posts/1" | |
7 | + | response = requests.get(url) | |
8 | + | ||
9 | + | if response.status_code == 200: | |
10 | + | print("Data retrieved successfully:") | |
11 | + | print(response.json()) | |
12 | + | else: | |
13 | + | print("Failed to retrieve data. Status code:", response.status_code) | |
14 | + | ||
15 | + | if __name__ == "__main__": | |
16 | + | get_data() | |
17 | + | ``` | |
18 | + | ||
19 | + | ||
20 | + |