GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python venv - virtual environments in Python using venv Python venvlast modified January 29, 2024 Python venv tutorial shows how to user virtual environments in Python using the venv module. Virtual environment
Complex applications rely on a number of different dependencies. Various
applications may depend on a library, or different versions of the same library.
Since Python cannot differentiate between versions in its
IDEs including Visual Code and PyCharm have extensive support for virtual environments. A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python and a number of additional packages.
Common installation tools such as Python venv
The Python create virtual environmentNext we show how to create a virtual environment. $ python --version Python 3.8.5 $ python -m venv myenv . myenv/bin/activate These commands create and activate a new virtual environment on Linux. > py --version Python 3.8.1 > py -m venv myenv > myenv\Scripts\activate These commands create and activate a new virtual environment on Windows. $ ls myenv/ bin include lib lib64 pyvenv.cfg share
The
The $ cat myenv/pyvenv.cfg home = /usr/bin include-system-site-packages = false version = 3.8.5
This is a sample output of the Python activate virtual environmentBefore we can use a virtual environment, we need to activate it first. $ python -m venv myenv
We create a new virtual environment called $ . myenv/bin/activate (myenv) $ We activate the virtual environment. We can see the name of the environment in the prompt. (myenv) $ echo $PATH /janbodnar/Documents/prog/python/venv/myenv/bin:/janbodnar/.cargo/bin/:/janbodnar/ Documents/prog/go-workspace/bin/:/janbodnar/.config/composer/vendor/bin/: ...
The (myenv) $ which pip python /root/Documents/prog/python/venv/myenv/bin/pip /root/Documents/prog/python/venv/myenv/bin/python
The (myenv) $ deactivate $
With the Python venv installing packages
After activating the created virtual environment, the (myenv) $ pip list Package Version ------------- ------- pip 20.0.2 pkg-resources 0.0.0 setuptools 46.1.3
We show the available packages with the (myenv) $ pip install requests urllib3
Into our new environment, we install the (myenv) $ pip list Package Version ------------- --------- certifi 2020.6.20 chardet 3.0.4 idna 2.10 pip 20.0.2 pkg-resources 0.0.0 requests 2.24.0 setuptools 46.1.3 urllib3 1.25.10
We list the available packages again. In addition to the (myenv) $ pip freeze --local | xargs pip uninstall -y To go to a clean state and remove all installed packages, we can execute this command. (myenv) $ deactivate $
To jump out of the virtual environment, we use the $ pip list Package Version ---------------------------- -------------- adblockparser 0.7 AdvancedHTTPServer 2.2.0 aiocmd 0.1.2 aiodns 2.0.0 aiohttp 3.6.2 aioredis 1.3.1 aiowinreg 0.0.3 ajpy 0.0.4 alembic 1.4.2.dev0 aniso8601 8.0.0 apispec 3.3.1 apispec-webframeworks 0.5.2 APScheduler 0.0.0 asciitree 0.3.3 asn1crypto 1.4.0 ... Outside of a virtual environment, we refer to the standard Python installation. It has naturally many more packages. $ rm -rf myenv/ If we do not need the virtual environment anymore, we can simply remove its directory. SourcePython venv - creation of virtual environments
In this article we have created Python virtual environments using AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |