{"id":553,"date":"2020-11-10T10:00:27","date_gmt":"2020-11-10T09:00:27","guid":{"rendered":"https:\/\/www.kth.se\/blogs\/pdc\/?p=553"},"modified":"2025-05-06T11:52:42","modified_gmt":"2025-05-06T09:52:42","slug":"working-with-python-virtual-environments","status":"publish","type":"post","link":"https:\/\/www.kth.se\/blogs\/pdc\/2020\/11\/working-with-python-virtual-environments\/","title":{"rendered":"Working with Python Virtual Environments"},"content":{"rendered":"<div class=\"post-content-wrapper\"><hr \/>\n<p><em>Note: This post has been updated to reflect the modules on Dardel (December 2021).<\/em><\/p>\n<hr \/>\n<p class=\"p1\">When we use Python in our work or personal projects, it is often necessary to use a number of packages that are not distributed as standard Python libraries. We therefore need to install those packages based on the specific requirements of every project. In the scenario of working on multiple projects, it is not uncommon that different projects have conflicting requirements in terms of packages. For example, project A may require version 1.0 of a certain package, while project B may require version 2.0 of the same package. A solution to this conflict is to separate the packages for different projects or purposes with the help of a so-called <a href=\"https:\/\/docs.python.org\/3\/glossary.html#term-virtual-environment\">\u201cvirtual environment\u201d<\/a>.<\/p>\n<p class=\"p1\">A Python virtual environment is an isolated run-time environment that makes it possible to install and execute Python packages without interfering with the outside world. Without a virtual environment, Python packages are installed either in the system site directory, which can be located via the following command:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">$ python -c 'import site; print(site.getsitepackages())'\r\n<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">or in the so-called Python user base, which is usually in the \u201c<code>$HOME\/.local<\/code>\u201d folder. A Python package installed in this way can have only one version, and it is therefore not possible to work with two or more projects that have conflicting requirements regarding the versions of a certain Python package. With the help of a virtual environment, we can have different Python site directories for different projects and have those site directories isolated from each other and from the system site directory.<\/p>\n<p>This blog post will briefly introduce two ways of creating and managing a Python virtual environment: <code>venv<\/code> or <code>conda<\/code>.<\/p>\n<p><!--more--><\/p>\n<h2>Virtual environment with venv<\/h2>\n<p class=\"p1\">The <a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\"><code>venv<\/code><\/a> module is a standard Python module that has been available since Python 3.3. It provides a lightweight virtual environment and is very straightforward to use.<\/p>\n<p class=\"p1\">For example, if we want to use the <code>venv<\/code> module on <a href=\"https:\/\/www.pdc.kth.se\/hpc-services\/computing-systems\/dardel-1.1043529\">Dardel<\/a>, we can first load the <code>anaconda<\/code> module, and then create a virtual environment in the <code>home<\/code> folder\u00a0 as follows. (Note: If you do this, you need to replace \u201c<code>username<\/code>\u201d in the example with your username and \u201c<code>u<\/code>\u201d with\u00a0 the first letter of your username.)<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">$ module load PDC\/21.11\r\n$ module load Anaconda3\/2021.05\r\n$ cd \/cfs\/klemming\/home\/u\/username\r\n$ python3 -m venv my-venv-dardel<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">After the \u201c<code>python3 -m venv ...<\/code>\u201d command has been executed, a new folder (named \u201c<code>my-venv-dardel<\/code>\u201d) will have been created. This folder will contain the files that are used by the virtual environment, such as executables, libraries and scripts. (Note: You can use a different name for the environment, and hence the new folder.\u00a0)<\/p>\n<p class=\"p1\">To use the new virtual environment, we need to activate it first as follows:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">$ source my-venv-dardel\/bin\/activate\r\n(my-venv-dardel) $<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span class=\"s1\">After the \u201c<code>source ...<\/code>\u201d command has been run, we will see <\/span><span class=\"s2\">the environment name surrounded by parentheses, which reminds us that the \u201c<code>my-venv-dardel<\/code>\u201d environment is currently active.<\/span><\/p>\n<p class=\"p1\"><span class=\"s1\">We can now check that the Python site directory using the following commands. In the output we can see that the <span class=\"s2\">\u201c<code>site-packages<\/code>\u201d folder is inside the \u201c<code>my-venv-dardel<\/code>\u201d folder:<\/span><\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-venv-dardel) $ python3 -c 'import site; print(site.getsitepackages())'\r\n['\/cfs\/klemming\/home\/u\/username\/my-venv-dardel\/lib\/python3.8\/site-packages']<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span class=\"s1\">We can also install any necessary packages in the now-active virtual environment. For example we can install <span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>yapf<\/code><\/span><\/span> (a formatter for Python files):<\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-venv-dardel) $ python3 -m pip install yapf<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span style=\"font-size: 1.125rem;\">and then check that <span class=\"s1\"><span class=\"s2\"><code>yapf<\/code><\/span><\/span> is installed under <span class=\"s2\">\u201c<code>my-venv-dardel<\/code>\u201d:<\/span><\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-venv-dardel) $ which yapf\r\n\/cfs\/klemming\/home\/u\/username\/my-venv-dardel\/bin\/yapf<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span class=\"s1\">To deactivate the virtual environment, use the <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>deactivate<\/code><\/span><\/span>\u201d command:<\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-venv-dardel) $ deactivate\r\n$<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span class=\"s1\">After the <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>deactivate<\/code><\/span><\/span>\u201d command has been run, the <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>my-venv-dardel<\/code>\u201d environment will become inactive, as indicated by the disappearance of <\/span><\/span>the environment name surrounded by parentheses<span style=\"font-size: 1.125rem;\"><span class=\"s2\">.<\/span><\/span><\/span><\/p>\n<h2>Virtual environment with conda<\/h2>\n<div class=\"highlight-default\">\n<p class=\"p1\">With the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>anaconda<\/code><\/span><\/span><\/span> module loaded on <a href=\"https:\/\/www.pdc.kth.se\/hpc-services\/computing-systems\/dardel-1.1043529\">Dardel<\/a>, it is also natural to use <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda<\/code>\u201d<\/span><\/span><\/span> to manage virtual environments for Python. <a href=\"https:\/\/docs.conda.io\/projects\/conda\/en\/latest\/user-guide\/index.html\"><span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span><\/a> is an open-source package and environment manager, and is included in both Anaconda and Miniconda.<\/p>\n<p class=\"p1\">To use <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span>, we need to initialize it first. This is often done by modifying the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>~\/.bashrc<\/code><\/span><\/span><\/span> script; however, the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>~\/.bashrc<\/code><\/span><\/span><\/span> script is automatically loaded upon login, and you may not want to initialize <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span> every time you log in to <a href=\"https:\/\/www.pdc.kth.se\/hpc-services\/computing-systems\/dardel-1.1043529\">Dardel<\/a>. In practice it is recommended to use a separate script for initializing <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span>. We can create a <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>~\/.bashrc.conda.dardel<\/code><\/span><\/span><\/span> file with the contents in the example below, which can then be used to initialize <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span> for Dardel.<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">module load <span style=\"color: #40015a;\">PDC\/21.11<\/span>\r\nmodule load <span style=\"color: #40015a;\">Anaconda3\/2021.05<\/span>\r\n\r\n<span style=\"color: #696969;\"># &gt;&gt;&gt; conda initialize &gt;&gt;&gt;<\/span>\r\n<span style=\"color: #696969;\"># <\/span><span style=\"color: #696969;\">!! Contents within this block are managed by 'conda init' !!<\/span>\r\n<span style=\"color: #797997;\">__conda_setup<\/span><span style=\"color: #808030;\">=<\/span><span style=\"color: #0000e6;\">\"$('<\/span><span style=\"color: #40015a;\">\/pdc\/software\/21.11\/eb\/software\/Anaconda3\/2021.05\/bin\/conda<\/span><span style=\"color: #0000e6;\">' 'shell.bash' 'hook' 2&gt; <\/span><span style=\"color: #40015a;\">\/dev\/null<\/span><span style=\"color: #0000e6;\">)\"<\/span>\r\n<span style=\"color: #800000; font-weight: bold;\">if<\/span> <span style=\"color: #808030;\">[<\/span> <span style=\"color: #797997;\">$?<\/span> <span style=\"color: #44aadd;\">-eq<\/span> <span style=\"color: #008c00;\">0<\/span> <span style=\"color: #808030;\">]<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800000; font-weight: bold;\">then<\/span>\r\n    <span style=\"color: #bb7977; font-weight: bold;\">eval<\/span> <span style=\"color: #0000e6;\">\"<\/span><span style=\"color: #797997;\">$__conda_setup<\/span><span style=\"color: #0000e6;\">\"<\/span>\r\n<span style=\"color: #800000; font-weight: bold;\">else<\/span>\r\n    <span style=\"color: #800000; font-weight: bold;\">if<\/span> <span style=\"color: #808030;\">[<\/span> <span style=\"color: #44aadd;\">-f<\/span> <span style=\"color: #0000e6;\">\"<span style=\"color: #40015a;\">\/pdc\/software\/21.11\/eb\/software\/Anaconda3\/2021.05\/<\/span><\/span><span style=\"color: #40015a;\">etc\/profile.d\/conda.sh<\/span><span style=\"color: #0000e6;\">\"<\/span> <span style=\"color: #808030;\">]<\/span><span style=\"color: #800080;\">;<\/span> <span style=\"color: #800000; font-weight: bold;\">then<\/span>\r\n        <span style=\"color: #800000; font-weight: bold;\">.<\/span> <span style=\"color: #0000e6;\">\"<span style=\"color: #40015a;\">\/pdc\/software\/21.11\/eb\/software\/Anaconda3\/2021.05\/<\/span><\/span><span style=\"color: #40015a;\">etc\/profile.d\/conda.sh<\/span><span style=\"color: #0000e6;\">\"<\/span>\r\n    <span style=\"color: #800000; font-weight: bold;\">else<\/span>\r\n        <span style=\"color: #bb7977; font-weight: bold;\">export<\/span> <span style=\"color: #797997;\">PATH<\/span><span style=\"color: #808030;\">=<\/span><span style=\"color: #0000e6;\">\"<span style=\"color: #40015a;\">\/pdc\/software\/21.11\/eb\/software\/Anaconda3\/2021.05\/<\/span><\/span><span style=\"color: #40015a;\">bin<\/span><span style=\"color: #0000e6;\">:<\/span><span style=\"color: #797997;\">$PATH<\/span><span style=\"color: #0000e6;\">\"<\/span>\r\n    <span style=\"color: #800000; font-weight: bold;\">fi<\/span>\r\n<span style=\"color: #800000; font-weight: bold;\">fi<\/span>\r\n<span style=\"color: #bb7977; font-weight: bold;\">unset<\/span> __conda_setup\r\n<span style=\"color: #696969;\"># &lt;&lt;&lt; conda initialize &lt;&lt;&lt;<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">After this we can load the anaconda module and initialize <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span> with the following command:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">$ source ~\/.bashrc.conda.dardel\r\n(base) $<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">After the above command has been executed, we&#8217;ll see <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>base<\/code>\u201d<\/span><\/span><\/span> surrounded by parentheses, indicating that the default virtual environment is active.<\/p>\n<p class=\"p1\">The next step is to configure <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span> with user-defined directories. For example, we can edit the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>~\/.condarc<\/code><\/span><\/span><\/span> file (or create the file if it doesn&#8217;t exist) so it contains the following lines.<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">pkgs_dirs:\r\n    - \/cfs\/klemming\/home\/u\/username\/conda-dirs\/pkgs\r\nenvs_dirs:\r\n    - \/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\r\n<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">Now we are ready to create our own virtual environment with <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span>. In practice, we use the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda create ...<\/code>\u201d command <\/span><\/span><\/span>and specify both the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<\/span><\/span><\/span><span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>--prefix<\/code>\u201d<\/span><\/span><\/span> option and the Python version with the following command:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(base) $ conda create --prefix \/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\/my-conda-dardel<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">Note that the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<\/span><\/span><\/span><span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>--prefix<\/code>\u201d<\/span><\/span><\/span> option should point to the virtual environment that is under the specified environment directory (<span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>env_dirs<\/code><\/span><\/span><\/span>) in the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>~\/.condarc<\/code><\/span><\/span><\/span> file. The output will look like the following:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">Collecting package metadata: done\r\nSolving environment: done\r\n\r\n## Package Plan ##\r\n\r\n  environment location: \/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\/my-conda-dardel\r\n\r\n  added \/ updated specs:\r\n    - python=3.8\r\n\r\nThe following packages will be downloaded:\r\n    ...\r\n\r\nThe following NEW packages will be INSTALLED:\r\n  ...\r\n\r\nProceed ([y]\/n)?\r\n<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">After pressing <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201cy\u201d<\/span><\/span><\/span> and the enter key, we\u2019ll see the following:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">Downloading and Extracting Packages\r\n...\r\n\r\nPreparing transaction: done\r\nVerifying transaction: done\r\nExecuting transaction: done\r\n\r\n# To activate this environment, use\r\n#\r\n# $ conda activate \/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\/my-conda-dardel\r\n#\r\n# To deactivate an active environment, use\r\n#\r\n# $ conda deactivate<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">With <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span>, the commands to activate and deactivate a virtual environment are <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda activate ...<\/code>\u201d<\/span><\/span><\/span>\u00a0and <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda deactivate<\/code>\u201d<\/span><\/span><\/span>, respectively. Since we have already specified <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>envs_dir<\/code><\/span><\/span><\/span> in <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>~\/.condarc<\/code><\/span><\/span><\/span>, we can activate the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>my-conda-dardel<\/code><\/span><\/span><\/span> environment using this command:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(base) $ conda activate my-conda-dardel\r\n(my-conda-dardel) $<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">We can check that the Python site directory is now under <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>my-conda-dardel<\/code> as follows:<\/span><\/span><\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-conda-dardel) $ python3 -c 'import site; print(site.getsitepackages())'\r\n['\/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\/my-conda-dardel\/lib\/python3.8\/site-packages']<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\">We can also install <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>yapf<\/code><\/span><\/span><\/span>, now using \u201c<span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda install<\/code><\/span><\/span><\/span>\u201d as in the following example (and by pressing the <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201cy\u201d<\/span><\/span><\/span> and enter keys to proceed):<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-conda-dardel) $ conda install yapf\r\n\r\nCollecting package metadata: done\r\nSolving environment: done\r\n\r\n## Package Plan ##\r\n\r\n  environment location: \/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\/my-conda-dardel\r\n\r\n  added \/ updated specs:\r\n    - yapf\r\n\r\nThe following packages will be downloaded:\r\n    ...\r\n\r\nThe following NEW packages will be INSTALLED:\r\n  ...\r\n\r\nProceed ([y]\/n)? y\r\n\r\nDownloading and Extracting Packages\r\n...\r\n\r\nPreparing transaction: done\r\nVerifying transaction: done\r\nExecuting transaction: done<\/pre>\n<\/div>\n<\/div>\n<p><span style=\"color: #000000; font-family: Georgia, 'garamond pro', garamond, 'times new roman', times, serif; font-size: 1.125rem;\">We can then check that <\/span><span class=\"s1\" style=\"color: #000000; font-family: Georgia, 'garamond pro', garamond, 'times new roman', times, serif; font-size: 1.125rem;\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>yapf<\/code><\/span><\/span><\/span><span style=\"color: #000000; font-family: Georgia, 'garamond pro', garamond, 'times new roman', times, serif; font-size: 1.125rem;\"> has been installed under <\/span><span class=\"s1\" style=\"color: #000000; font-family: Georgia, 'garamond pro', garamond, 'times new roman', times, serif; font-size: 1.125rem;\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>my-conda-dardel<\/code><span style=\"color: #000000; font-family: Georgia, 'garamond pro', garamond, 'times new roman', times, serif; font-size: 1.125rem;\"> using the following command:<\/span><\/span><\/span><\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-conda-dardel) $ which yapf\r\n\/cfs\/klemming\/home\/u\/username\/conda-dirs\/envs\/my-conda-dardel\/bin\/yapf\r\n<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span class=\"s1\">To deactivate the virtual environment, use the <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda deactivate<\/code>\u201d <\/span><\/span>command:<\/span><\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(my-conda-dardel) $ conda deactivate\r\n(base) $<\/pre>\n<\/div>\n<\/div>\n<p class=\"p1\"><span class=\"s1\">After the <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda deactivate<\/code><\/span><\/span>\u201d command has been run, the <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>my-conda-dardel<\/code>\u201d <\/span><\/span>environment will become inactive and the default <span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>base<\/code>\u201d <\/span><\/span>environment will become active again. <\/span>The <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\">\u201c<code>conda deactivate<\/code><\/span><\/span>\u201d command can also be used to<\/span> deactivate the base environment as follows:<\/p>\n<div class=\"highlight-default\">\n<div class=\"highlight\">\n<pre style=\"color: #000000; background: #ffffff;\">(base) $ conda deactivate\r\n$<\/pre>\n<\/div>\n<\/div>\n<h2>Summary<\/h2>\n<p>We have briefly introduced the creation and management of a Python virtual environment using <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>venv<\/code><\/span><\/span><\/span> and <span class=\"s1\"><span style=\"font-size: 1.125rem;\"><span class=\"s2\"><code>conda<\/code><\/span><\/span><\/span>. The former is lightweight and easy-to-use while the latter is more powerful and versatile. You can find more information about them in the following links:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/venv.html\">https:\/\/docs.python.org\/3\/library\/venv.html<\/a><\/li>\n<li><a href=\"https:\/\/docs.conda.io\/projects\/conda\/en\/latest\/user-guide\/index.html\">https:\/\/docs.conda.io\/projects\/conda\/en\/latest\/user-guide\/index.html<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Note: This post has been updated to reflect the modules on Dardel (December 2021). When we use Python in our work or personal projects, it is often necessary to use a number of packages that are not distributed as standard Python libraries. We therefore need to install those packages based on the specific requirements of [&hellip;]<\/p>\n","protected":false},"author":1140,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"footnotes":""},"categories":[6],"tags":[19,24],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-environment-management","tag-python","tag-virtual-environment"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p9W9Im-8V","_links":{"self":[{"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/users\/1140"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/comments?post=553"}],"version-history":[{"count":31,"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/posts\/553\/revisions\/589"}],"wp:attachment":[{"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kth.se\/blogs\/pdc\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}