Otu esi edozi Python 3 Virtual Environment na Ubuntu 20.04

Python's venv modul bụ gburugburu ebe obibi bụ ebe Python dị ka onye ntụgharị Python, ọba akwụkwọ na script etinyere n'ime ya dịpụrụ adịpụ site na ndị arụnyere na mpaghara ndị ọzọ mebere, na (na ndabara) ọba akwụkwọ ọ bụla arụnyere na sistemụ arụmọrụ gị, dịka ọmụmaatụ, ndị etinyere na sistemụ arụmọrụ Ubuntu gị ka ịzenarị ọgụ na ịkpaghasị gburugburu ebe nrụpụta gị.

N'ime nkuzi a, ị ga-amụta ka esi etinye ya Python 3 na PIP 3 on your Ubuntu 20.04 sistemụ arụmọrụ, yana ịtọlite ​​​​mgbe mmemme site na ahịrị iwu.

Prerequisites

  • OS akwadoro: Ubuntu 20.04 - nhọrọ (Ubuntu 21.04)
  • Akaụntụ onye ọrụ: Akaụntụ onye ọrụ nwere sudo ma ọ bụ ohere mgbọrọgwụ.
  • Ngwungwu achọrọ: Python 3 na PIP 3

Wụnye nrụpụ

Maka nkuzi na ịmepụta gburugburu Python 3, ị ga-achọ ịwụnye ndabere ndị a na njedebe Ubuntu gị (Ctrl+ALT+T):

sudo apt install -y build-essential libssl-dev libffi-dev

Advertisement


Wụnye Python 3.8 (Ubuntu Default) ma ọ bụ Python 3.9

Site na ndabara, Python 3.8 na-abịa na ebe nchekwa Ubuntu 20.04. Iji tinye Python 3.9, mee iwu a:

sudo apt install python3.9

Ọmụmatụ nke ndabere nke a ga-etinyekwa:

Otu esi edozi Python 3 Environment (VENV) na Ubuntu 20.04

Ụdị Y, wee pịa tinye igodo ịga n'ihu na nwụnye.

Jiri ụdị jiri kwado nrụnye ma wuo iwu:

python3.9 --version

Ihe atụ mmepụta:

Python 3.9.5

Ọ bụrụ na ị ga-achọ ụdị Python 3.9 ọhụrụ, wụnye PPA ppa: agwọ nwụrụ anwụ/ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update

Ugbu a, ọ bụrụ na ị nwere Python 3 dị, ị nwere ike kwalite, ma ọ bụ ọ bụrụ na ị na-etinye nke ọhụrụ, na-agba ọsọ iwu wụnye:

Iji wụnye:

sudo apt install python3.9 -y

Iji kwalite dị:

sudo apt upgrade python3.9 -y

Ozugbo emechara, lelee ihe owuwu ọhụrụ ahụ ma e jiri ya tụnyere nke ebe nchekwa ndabere Ubuntu:

python3.9 --version

Ihe atụ mmepụta:

3.9.6

Were ya na ị ga-amasị Python 3.10 ọhụrụ; biko hụ ntuziaka anyị maka ịwụnye Python 3.10 na Ubuntu 20.04. Mara, nke a kwesịrị iji naanị maka ndị mmepe nwere ahụmahụ chọrọ ihe kachasị ọhụrụ dịka 3.10 na ederede ka dị na BETA.

Nhọrọ - python3-dev

Python-dev bụ ngwugwu nwere faịlụ nkụnye eji isi mee maka Python C API, nke lxml na-eji maka na ọ gụnyere mgbakwunye Python C maka ịrụ ọrụ dị elu. Python-dev nwere ihe niile achọrọ iji chịkọta modul ndọtị Python

Ndị ọrụ Python 3.8:

sudo apt install python3-dev

Ndị ọrụ Python 3.9:

sudo apt install python3.9-dev

Wụnye PIP 3 na Ubuntu 20.04

Mgbe ị na-arụ ọrụ na Python 3, ị ga-achọ ụzọ isi jikwaa ngwugwu ngwanrọ. Ngwá ọrụ dị mma iji bụ Pip nke nwere ike ịwụnye ma jikwaa ngwugwu. Enwere ike ịchọta ntuziaka dị omimi na ịwụnye Pip na Ubuntu 20.04 na Otu esi etinye PIP2 na PIP 3 na Ubuntu 20.04.

Iji wụnye Pip maka Python 3.8, nke na-abịa dị ka ndabara na Ubuntu 20.04, jiri iwu a:

sudo apt install pip3

Ọmụmaatụ nke ndabere nke a ga-etinye:

Otu esi edozi Python 3 Environment (VENV) na Ubuntu 20.04

Ụdị Y, wee pịa tinye igodo ịga n'ihu na nwụnye.

Nyochaa nwụnye Pip site na iji iwu a:

pip3 --version

Ihe atụ mmepụta:

pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

Iji tinye PIP maka Python 3.9 ma ọ bụ karịa, jiri iwu curl a:

curl -sSL https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

Mgbe e gburu ya, nweta-pip.py ga-etinye pip, ntọala ntọala, na wheel na gburugburu Python ugbu a.

Nyochaa nrụnye na Pip maka Python 3.9 ziri ezi:

python3.9 --version

Ihe atụ mmepụta:

pip 21.2.4 from /home/joshua/.local/lib/python3.9/site-packages/pip (python 3.9)

Advertisement


Mepụta Python 3 Environment na Ubuntu 20.04

Ihe mbụ ị ga-achọ ịtọ ntọala python 3 gburugburu bụ ịwụnye ngwugwu ahụ Python3-env.

Iji mee nke a, jiri iwu na-esonụ maka Python 3.8:

sudo apt install python3-venv

Maka ndị ọrụ Python 3.9:

sudo apt install python3.9-venv

Na-esote, mepụta akwụkwọ ndekọ aha ọhụrụ iji kwado gburugburu Python anyị site na iji iwu mkdir:

sudo mkdir myproject
cd myproject

Mara, na myproject Enwere ike iji ihe ọ bụla masịrị gị dochie aha ndekọ aha.

N'ime ndekọ aha, mee iwu a ka ịmepụta gburugburu:

Ndị ọrụ Python 3.8:

python3 -m venv myapplication

Ndị ọrụ Python 3.9:

python3.9 -m venv myapplication

Ozugbo n'ime gburugburu ebe ị ka kere, iwu ahụ na-edozi ndekọ ahụ, nke nwere ike iji akwụkwọ ahụ nyochaa ndekọ ahụ ls iwu:

ls myapplication

Ihe atụ mmepụta:

joshua@ubuntu:~/myproject$ ls myapplication/
bin  include  lib  lib64  pyvenv.cfg

Iji mee ka gburugburu ebe obibi rụọ ọrụ ma ọ bụ, n'okwu dị mfe, banye na gburugburu ebe obibi, jiri iwu a:

source myapplication/bin/activate

Mgbe ịmechara gburugburu gị, ị ga-achọpụta na njikwa iwu gị ga-abụ prefix na aha gburugburu, nke a na-akpọ aha (Myapplication) dịka ọmụmaatụ nkuzi anyị.

Otu ihe atụ nke a bụ n'okpuru:

(myapplication) joshua@ubuntu:~/myproject$ 

Mepụta Mmemme Nnwale (Ndewo Ụwa)

Iji nwalee ebe mebere gị, ụzọ dị ngwa bụ imepụta mmemme ihe atụ Hello World ama ama. Ime nke a ga-eme ka ihe niile na-arụ ọrụ ma mee ka ị mara na ị na-arụ ọrụ na gburugburu Python.

Na gburugburu gị, mepụta ma mepee edemede Python gị n'ọdịnihu:

sudo nano hello.py

Na faịlụ, detuo na mado koodu a:

print("Hello, World!")

Chekwaa faịlụ (CTRL+O), wee pụọ (CTRL+X).

Ugbu a mee faịlụ ule Python site na iji iwu a:

Ndị ọrụ Python 3.8:

python hello.py

Ndị ọrụ Python 3.9:

python3.9 hello.py

Ọ bụrụ na ihe niile na-arụ ọrụ nke ọma, ị ga-enweta nsonaazụ ndị a:

Hello, World!

Mgbe ịmechara na mpaghara my_env, ịpụ pịnye:

deactivate

Advertisement


Okwu na mmechi

N'ime nkuzi a, ị mụtala ka esi etinye Python 3. N'ozuzu, Python mebere gburugburu na-enyere aka decouple na ikewapụ Python na ngwugwu pip jikọtara ya, na-enye ndị ọrụ njedebe ohere ịwụnye na jikwaa nchịkọta nke ha onwe ha na-adabere na nke ndị nyere site na ya. usoro. Nke a dị ezigbo mma mgbe ị na-emepụta faịlụ sistemu iche iche.

Ahapụ a Comment