close

There are both English and Chinese version of instructions below.

以下分為中文及英文版的解說

 

中文版:

1. 安裝 esay-install

指令: pip install virtualenv

2. 創建虛擬環境

指令: virtualenv environment_name

3. 啟用虛擬環境 (Linux版)

首先,cd到剛剛創建的虛擬環境資料夾下

接著輸入以下指令

指令: source bin/activate

 

3. 啟用虛擬環境 (Windows版)

首先,cd到剛剛創建的虛擬環境資料夾下

接著輸入以下指令

指令: Scripts\activate.bat

這時你可以看到命令列的開頭多出了一串括號的字,代表虛擬環境已經成功啟用

 

4. 終止虛擬環境

指令: deactivate

這時你可以看到原本命另列開頭的括號的那一串字不見了,代表你已經成功終止虛擬環境的使用了~

 

當你啟用虛擬環境後,所有你用pip指令下載的東西都只會存在在虛擬環境裡,是不是很方便呢?


 

 

English version:

1. install esay-install

Command: pip install virtualenv

2.Create a virtual environment

Command: virtualenv environment_name

3. Activate the virtual environment (Under Linux)

First, cd to the virtual environment directory.

Then use the following command to activate the environment.

Command: source bin/activate

 

3. Activate the virtual environment (Under Windows)

First, cd to the virtual environment directory.

Then type the following command in command line:

Command: Scripts\activate.bat

By the parentheses at the prefix you can see that your virtual environment is already activated!

 

4. Deactivate the virtual environment

Command: deactivate

We can see that the virtual environment is deactivated by the change of the prefix.

 

Once you activate the virtual environment, everything that you install by the command pip will only be in that virtual environment.

 

[More!] List environments and Delete environments

If you want to see the list of the virtual environments you have right now, you have to install additional package called 'virtualenvwrapper'

First, we need to install it via pip

Command: pip install virtualenvwrapper

To activate virtualenvwrapper, add three lines in ~/.bashrc:

Don't forget to replace the three paths with your own.

(/home/kitty/virtualenv is where I'll create my virtual environments)

Then, source ~/.bashrc

[Note] You can find where your virtualenvwrapper.sh is by the following command:

Command: find . -name 'virtual*' (you can replace . with other directories)

Now let's test if it's working now~

First I create two virtual environments called 'test' and 'test2', respectively.

Then I use the command 'lsvirtualenv' to see what environments I have now.

You can also use '-b' option to see a brief version list.

Now, I want to delete test2

Command: rmvirtualenv environment_name

After that, I can use lsvirtualenv to see the list again, and now you can see that test2 is removed.

 

 

 

 

參考資料 reference:

1. About Python virtualenv:

https://www.openfoundry.org/tw/tech-column/8516-pythons-virtual-environment-and-multi-version-programming-tools-virtualenv-and-pythonbrew

2. How to install virtualenv:

https://virtualenv.pypa.io/en/stable/installation/

3. virtualenvwrapper

http://virtualenvwrapper.readthedocs.io/en/latest/install.html

4. virtualenv related commands:

https://howchoo.com/g/nwewzjmzmjc/a-guide-to-python-virtual-environments-with-virtualenvwrapper

5. lsvirtualenv "long" and"brief" options

https://stackoverflow.com/questions/7212140/list-all-virtualenv

arrow
arrow
    全站熱搜

    慈 發表在 痞客邦 留言(0) 人氣()