Vào tháng 12 năm 2019, chúng tôi đã phát hành một bài đăng trên blog nói về cách chúng tôi sẽ đối phó với cuộc sống cuối cùng của Python 2. Kể từ đó, đã có khá nhiều công cụ mà người dùng sử dụng chưa được chuyển đến Python 3, gây ra vấn đề khi họ cố gắng sử dụng chúng. Trang này sẽ bao gồm một cách sử dụng phiên bản khấu hao một cách an toàn.
Pyenv
Python 2 không còn được duy trì trong các kho lưu trữ Debian. Điều này có nghĩa là chúng ta phải tìm cách giải quyết vấn đề này. pyenv
giải quyết vấn đề này bằng cách cho phép chúng tôi cài đặt nhiều phiên bản Python không xung đột với nhau. Hiện tại nó không có trong kho Debian hoặc Kali, vì vậy chúng tôi sẽ cần cài đặt nó từ nguồn. Rất may, có một tập lệnh cài đặt tiện dụng mà các tác giả đã phát hành. Hãy cùng đi qua cài đặt và thiết lập cùng nhau.
[email protected]:~$ sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
[...]
[email protected]:~$
Tiếp theo chúng tôi sẽ nhanh chóng chạy tập lệnh cài đặt bash. Nếu ZSH
là shell mặc định, chúng ta sẽ phải chỉnh sửa tệp .zshrc
sau đó.
[email protected]:~$ curl //pyenv.run | bash
[...]
[email protected]:~$
Nếu chúng tôi đang sử dụng ZSH
thì bây giờ chúng tôi sẽ thêm các dòng thích hợp vào .zshrc
của chúng tôi.
[email protected]:~$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
[email protected]:~$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
[email protected]:~$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$[pyenv init --path]"\nfi' >> ~/.zshrc
Hãy tiếp tục với thiết lập
[email protected]:~$ exec $SHELL
[email protected]:~$
[email protected]:~$ pyenv
pyenv 1.2.20
Usage: pyenv []
Some useful pyenv commands are:
activate Activate virtual environment
commands List all available pyenv commands
deactivate Deactivate virtual environment
doctor Verify pyenv installation and development tools to build pythons.
exec Run an executable with the selected Python version
global Set or show the global Python version[s]
help Display help for a command
hooks List hook scripts for a given pyenv command
init Configure the shell environment for pyenv
install Install a Python version using python-build
local Set or show the local application-specific Python version[s]
prefix Display prefix for a Python version
rehash Rehash pyenv shims [run this after installing executables]
root Display the root directory where versions and shims are kept
shell Set or show the shell-specific Python version
shims List existing pyenv shims
uninstall Uninstall a specific Python version
--version Display the version of pyenv
version Show the current Python version[s] and its origin
version-file Detect the file that sets the current pyenv version
version-name Show the current Python version
version-origin Explain how the current Python version is set
versions List all Python versions available to pyenv
virtualenv Create a Python virtualenv using the pyenv-virtualenv plugin
virtualenv-delete Uninstall a specific Python virtualenv
virtualenv-init Configure the shell environment for pyenv-virtualenv
virtualenv-prefix Display real_prefix for a Python virtualenv version
virtualenvs List all Python virtualenvs found in `$PYENV_ROOT/versions/*'.
whence List all Python versions that contain the given executable
which Display the full path to an executable
See `pyenv help ' for information on a specific command.
For full documentation, see: //github.com/pyenv/pyenv#readme
[email protected]:~$
Bây giờ chúng ta có thể cài đặt Python 2 và đặt nó thành phiên bản Python mặc định của chúng ta:
[email protected]:~$ pyenv install 2.7.18
Downloading Python-2.7.18.tar.xz...
-> //www.python.org/ftp/python/2.7.18/Python-2.7.18.tar.xz
Installing Python-2.7.18...
Installed Python-2.7.18 to /home/kali/.pyenv/versions/2.7.18
[email protected]:~$
[email protected]:~$ pyenv global 2.7.18
[email protected]:~$
[email protected]:~$ pyenv versions
system
* 2.7.18 [set by /home/kali/.pyenv/version]
[email protected]:~$
[email protected]:~$ python
Python 2.7.18 [default, Apr 20 2020, 20:30:41]
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[email protected]:~$
Bây giờ chúng ta có thể cài đặt các phụ thuộc khi cần thiết cho bất kỳ công cụ nào chúng ta đang sử dụng. Khi chúng tôi muốn trao đổi trở lại Python 3, chúng tôi chỉ cần thiết lập toàn cầu thành hệ thống.
Một điều cần lưu ý là gắn bó với việc cài đặt các phụ thuộc thông qua
[email protected]:~$ curl //pyenv.run | bash
[...]
[email protected]:~$
0. [email protected]:~$ curl //pyenv.run | bash
[...]
[email protected]:~$
1 sẽ không tử tế nếu bạn đang cố gắng cài đặt các phụ thuộc Python 2 thông qua nó và thông qua PIP, vì vậy chỉ cần gắn bó với PIP trong trường hợp này.