今天,在在使用 pycharm 的使用,進行創建 python的時候,發現使用默認的創建的選項使用的python 3環境 。而我系統默認的python環境是 python 2.7 環境;這就引起了我的興趣。
我打開pycharm 的終端,發現:
前面 有個 venv 參數,通過 調研了一番我發現:python 的 venv 模塊可以創建一個獨立的虛擬的python運行環境,這樣就和系統的python獨立開來了。而我使用fedora 28的系統,默認安裝了python2.7 和 python3.6 兩種python環境。
我們使用 python 內置的文檔查看,venv 相關,其描述為:
Help on package venv:
NAME
venv - Virtual environment (venv) package for Python. Based on PEP 405.
我們使用python3 查看 venv 模塊的使用方法:
➜ env pwd/home/xuyaowen/Desktop/workplace/env➜ env python3 -m venv -husage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] [--upgrade] [--without-pip] [--prompt PROMPT] ENV_DIR [ENV_DIR ...]Creates virtual Python environments in one or more target directories.positional arguments: ENV_DIR A directory to create the environment in.optional arguments: -h, --help show this help message and exit --system-site-packages Give the virtual environment access to the system site-packages dir. --symlinks Try to use symlinks rather than copies, when symlinks are not the default for the platform. --copies Try to use copies rather than symlinks, even when symlinks are the default for the platform. --clear Delete the contents of the environment directory if it already exists, before environment creation. --upgrade Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place. --without-pip Skips installing or upgrading pip in the virtual environment (pip is bootstrapped by default) --prompt PROMPT Provides an alternative prompt prefix for this environment.Once an environment has been created, you may wish to activate it, e.g. bysourcing an activate script in its bin directory.
通過上面的介紹,我們大致知道 venv 的模塊使用方法:
首先我們創建虛擬環境:
➜ venvtest pwd/home/xuyaowen/Desktop/workplace/venvtest➜ venvtest python3 -m venv .
我們查看創建的結果:
➜ venvtest lsbin include lib lib64 pyvenv.cfg➜ venvtest ll *lrwxrwxrwx. 1 xuyaowen xuyaowen 3 Jul 27 11:44 lib64 -> lib-rw-r--r--. 1 xuyaowen xuyaowen 69 Jul 27 11:44 pyvenv.cfgbin:total 32K-rw-r--r--. 1 xuyaowen xuyaowen 2.2K Jul 27 11:44 activate-rw-r--r--. 1 xuyaowen xuyaowen 1.3K Jul 27 11:44 activate.csh-rw-r--r--. 1 xuyaowen xuyaowen 2.4K Jul 27 11:44 activate.fish-rwxr-xr-x. 1 xuyaowen xuyaowen 271 Jul 27 11:44 easy_install-rwxr-xr-x. 1 xuyaowen xuyaowen 271 Jul 27 11:44 easy_install-3.6-rwxr-xr-x. 1 xuyaowen xuyaowen 243 Jul 27 11:44 pip-rwxr-xr-x. 1 xuyaowen xuyaowen 243 Jul 27 11:44 pip3-rwxr-xr-x. 1 xuyaowen xuyaowen 243 Jul 27 11:44 pip3.6lrwxrwxrwx. 1 xuyaowen xuyaowen 7 Jul 27 11:44 python -> python3lrwxrwxrwx. 1 xuyaowen xuyaowen 16 Jul 27 11:44 python3 -> /usr/bin/python3include:total 0lib:total 4.0Kdrwxr-xr-x. 3 xuyaowen xuyaowen 4.0K Jul 27 11:44 python3.6
新聞熱點
疑難解答