Skip to content

Commit 5004040

Browse files
authored
Merge pull request #1 from HienDoan92/master
Bản dịch đầu tiên
2 parents 7bfdd96 + 65cdea6 commit 5004040

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

12.-virtual-environments-and-packages.md

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1-
# 12. Virtual Environments and Packages
1+
# 12. Môi trường phân lập (Virtual Environments) và các gói thư viện (Packages)
22

3-
### 12.1. Introduction
3+
### 12.1. Giới thiệu
44

5-
Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.
5+
Các chương trình viết bằng Python trên thực tế thường dùng các gói thư viện và module bên ngoài các thư viện - module có sẵn. Những chương trình này đôi khi cần phiên bản cụ thể của một thư viện nào đó.
66

7-
This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.
7+
Điều này có nghĩa là ít khi nào một bộ thư viện Python duy nhất phù hợp với tất cả các chương trình sử dụng nó. Nếu chương trình A cần phiên bản 1.0 của một module cụ thể nào đó nhưng chương trình B lại cần phiên bản 2.0 của module này, sự xung đột phiên bản sẽ xảy ra. Bạn chỉ có thể cài phiên bản 1.0 hoặc 2.0 của module trên một máy tính, do đó chương trình A hoặc B sẽ không chạy.
88

9-
The solution for this problem is to create a [virtual environment](https://docs.python.org/3/glossary.html#term-virtual-environment), a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
9+
Giải pháp xử lý vấn đề này là tạo ra [virtual environment](https://docs.python.org/3/glossary.html#term-virtual-environment), một thư mục chứa phiên bản cụ thể của một thư viện cụ thể, hoặc thậm chí chứa bộ thư viện chuẩn của Python theo phiên bản nào đó.
1010

11-
Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.
11+
Các chương trình khác nhau có thể sử dụng các môi trường phân lập (virtual ennviroment) khác nhau để thực thi. Để giải quyết vấn đề xung đột phiên bản thư viện, chương trình A có thể có môi trường phân lập riêng chứa phiên bản 1.0 của thư viện, còn chương trình B lại sử dụng môi trường phân lập khác chứa phiên bản thư viện 2.0. Nếu chương trình B cần nâng cấp phiên bản thư viện lên 3.0 trong virtual enviroment của nó, điều này sẽ không ảnh hưởng gì tới virtual enviroment của A.
1212

13-
### 12.2. Creating Virtual Environments
13+
### 12.2. Tạo môi trường phân lập (Virtual Environment)
1414

15-
The module used to create and manage virtual environments is called [`venv`](https://docs.python.org/3/library/venv.html#module-venv). [`venv`](https://docs.python.org/3/library/venv.html#module-venv) will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running `python3` or whichever version you want.
15+
Module được sử dụng để tạo và quản lý virtual enviroment có tên là [`venv`](https://docs.python.org/3/library/venv.html#module-venv). [`venv`](https://docs.python.org/3/library/venv.html#module-venv) thường sẽ cài đặt các thư viện Python mới nhất trên máy tính của bạn vào virtual enviroment. Nếu bạn có nhiều phiên bản Python trên máy tính, bạn có thể chọn lựa phiên bản Python bằng lệnh `python3` hoặc bất cứ phiên bản Python nào bạn muốn.
1616

17-
To create a virtual environment, decide upon a directory where you want to place it, and run the [`venv`](https://docs.python.org/3/library/venv.html#module-venv) module as a script with the directory path:
17+
Để tạo virtual enviroment, đầu tiên bạn chọn một thư mục, rồi gõ [`venv`](https://docs.python.org/3/library/venv.html#module-venv) vào OS shell:
1818

1919
```text
2020
python3 -m venv tutorial-env
2121
```
2222

23-
This will create the `tutorial-env` directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
23+
Lệnh này sẽ tạo thư mục `tutorial-env` nếu nó chưa tồn tại, cùng với đó là các thư mục con chứa trình thông dịch Python, các thư viện Python chuẩn và các file cần thiết khác.
2424

25-
Once you’ve created a virtual environment, you may activate it.
25+
Sau khi tạo virtual environment, bạn cần kích hoạt (activate) nó để sử dụng.
2626

27-
On Windows, run:
27+
Trên Windows, gõ lệnh:
2828

2929
```text
3030
tutorial-env\Scripts\activate.bat
3131
```
3232

33-
On Unix or MacOS, run:
33+
Trên Unix hoặc MacOS, gõ lệnh:
3434

3535
```text
3636
source tutorial-env/bin/activate
3737
```
3838

39-
\(This script is written for the bash shell. If you use the **csh** or **fish** shells, there are alternate `activate.csh` and `activate.fish` scripts you should use instead.\)
39+
\(Đoạn lệnh trên được viết cho bash shell. Nếu bạn sử dụng **csh** hoặc **fish** shell, sẽ có những script tương ứng là `activate.csh` `activate.fish` cho bạn sử dụng.\)
4040

41-
Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running `python` will get you that particular version and installation of Python. For example:
41+
Kích hoạt virtual environment sẽ chuyển điều khiển từ OS shell về shell của virtual enviroment bạn đang sử dụng, và lúc này bạn sẽ thấy các thư viện trên virtual enviroment hiện tại khác với thư viện trên virtual enviroment khác. Ví về sự khác biệt giữa phiên bản Python trong và ngoài virtual enviroment:
4242

4343
```text
4444
$ source ~/envs/tutorial-env/bin/activate
@@ -52,9 +52,9 @@ Python 3.5.1 (default, May 6 2016, 10:59:36)
5252
>>>
5353
```
5454

55-
### 12.3. Managing Packages with pip
55+
### 12.3. Quản lý các gói thư viện bằng pip
5656

57-
You can install, upgrade, and remove packages using a program called **pip**. By default `pip` will install packages from the Python Package Index, <[https://pypi.python.org/pypi](https://pypi.python.org/pypi)>. You can browse the Python Package Index by going to it in your web browser, or you can use `pip`’s limited search feature:
57+
Bạn có thể cài đặt, nâng cấp, xóa bỏ các gói thư viện bằng một chương trình tên là **pip**. Mặc định, `pip` sẽ cài đặt các gói thư viện nằm trên Python Package Index, <[https://pypi.python.org/pypi](https://pypi.python.org/pypi)>. Bạn có thể tìm hiểu các gói thư viện này bằng cách vào trang web của Python Package Index hoặc tìm kiếm thông qua lệnh `pip` trên shell:
5858

5959
```text
6060
(tutorial-env) $ pip search astronomy
@@ -66,9 +66,9 @@ PyAstronomy - A collection of astronomy related tools for Python.
6666
...
6767
```
6868

69-
`pip` has a number of subcommands: “search”, “install”, “uninstall”, “freeze”, etc. \(Consult the [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) guide for complete documentation for `pip`.\)
69+
`pip` có nhiều câu lệnh con: "search", "install", "uninstall", "freeze", v.v.. \(Tham khảo [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) để hiểu đầy đủ hơn về `pip`.\)
7070

71-
You can install the latest version of a package by specifying a package’s name:
71+
Bạn có thể cài phiên bản mới nhất của gói thư viện bất kỳ bằng cách nhập tên gói thư viện ấy vào shell:
7272

7373
```text
7474
(tutorial-env) $ pip install novas
@@ -79,7 +79,7 @@ Installing collected packages: novas
7979
Successfully installed novas-3.1.1.3
8080
```
8181

82-
You can also install a specific version of a package by giving the package name followed by `==` and the version number:
82+
Bạn có thể cài đặt một phiên bản cụ thể của gói thư viện bằng cách thêm dấu `==` và số phiên bản mong muốn:
8383

8484
```text
8585
(tutorial-env) $ pip install requests==2.6.0
@@ -89,7 +89,7 @@ Installing collected packages: requests
8989
Successfully installed requests-2.6.0
9090
```
9191

92-
If you re-run this command, `pip` will notice that the requested version is already installed and do nothing. You can supply a different version number to get that version, or you can run `pip install --upgrade` to upgrade the package to the latest version:
92+
Nếu bạn chạy lệnh dưới đây, `pip` sẽ thông báo phiên bản mà bạn mong muốn đã được cài đặt từ trước. Bạn có thể chỉ định cài đặt một phiên bản khác bằng việc bổ sung thêm số phiên bản sau tên thư viện, hoặc chạy `pip install --upgrade` để nâng cấp gói thư viện tới bản mới nhất:
9393

9494
```text
9595
(tutorial-env) $ pip install --upgrade requests
@@ -101,9 +101,9 @@ Installing collected packages: requests
101101
Successfully installed requests-2.7.0
102102
```
103103

104-
`pip uninstall` followed by one or more package names will remove the packages from the virtual environment.
104+
Lệnh `pip uninstall` đặt phía trước tên các package có tác dụng xóa các package này khỏi virtual enviroment.
105105

106-
`pip show` will display information about a particular package:
106+
Lệnh `pip show` đặt phía trước tên package sẽ hiển thị thông tin về package đó:
107107

108108
```text
109109
(tutorial-env) $ pip show requests
@@ -120,7 +120,7 @@ Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
120120
Requires:
121121
```
122122

123-
`pip list` will display all of the packages installed in the virtual environment:
123+
Lệnh `pip list` sẽ hiển thị tất cả các gói thư viện đã được cài đặt trong virtual enviroment:
124124

125125
```text
126126
(tutorial-env) $ pip list
@@ -131,7 +131,7 @@ requests (2.7.0)
131131
setuptools (16.0)
132132
```
133133

134-
`pip freeze` will produce a similar list of the installed packages, but the output uses the format that `pip install`expects. A common convention is to put this list in a `requirements.txt` file:
134+
Lệnh `pip freeze` cũng sẽ hiển thị tất cả các gói thư viện đã được cài đặt, nhưng kết quả trả về sẽ được trình bày sao cho phù hợp để làm đầu vào cho lệnh `pip install`. Thông thường kết quả này sẽ được ghi ra file `requirements.txt`:
135135

136136
```text
137137
(tutorial-env) $ pip freeze > requirements.txt
@@ -141,7 +141,7 @@ numpy==1.9.2
141141
requests==2.7.0
142142
```
143143

144-
The `requirements.txt` can then be committed to version control and shipped as part of an application. Users can then install all the necessary packages with `install -r`:
144+
File `requirements.txt` có thể được dùng như một file cài đặt của chương trình Python. Người dùng có thể cài đặt tất cả các gói thư viện cần thiết với bằng cách thêm tham số `install -r`:
145145

146146
```text
147147
(tutorial-env) $ pip install -r requirements.txt
@@ -156,5 +156,4 @@ Installing collected packages: novas, numpy, requests
156156
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
157157
```
158158

159-
`pip` has many more options. Consult the [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) guide for complete documentation for `pip`. When you’ve written a package and want to make it available on the Python Package Index, consult the [Distributing Python Modules](https://docs.python.org/3/distributing/index.html#distributing-index) guide.
160-
159+
`pip` còn nhiều tham số khác nữa cho bạn sử dụng. Tham khảo thêm [Installing Python Modules](https://docs.python.org/3/installing/index.html#installing-index) để biết rõ hơn về `pip`. Nếu bạn có một gói thư viện của riêng mình và muốn nó xuất hiện trên Python Package Index, tham khảo cách làm tại [Distributing Python Modules](https://docs.python.org/3/distributing/index.html#distributing-index).

0 commit comments

Comments
 (0)