Multiple activation of environment? - python

I made virtual environment by using venv module and activated it.
Before I activated it, (base) D:\Python\venv was shown in terminal. Since I only installed Anaconda and VSCode, I guess this (base) environment was from Anaconda3.
After I activated the virtual environment, it was added in front of (base) interpreter like this : (venv) (base) D:\Python\venv. Does this mean both environments are in activation? For check, when I typed pip list, it only showed a list of (venv) environment. Then why does (base) environment show? Is it okay not to deactivate (base) when I use (venv)? I'm confused.

Yes, you've created multiple virtual environments.
When you start out in the "virtual environments world"it can be quite confusing.
In fact, you can create virtual environments in various ways.
To create a virtual environment, you can use for example :
conda
venv
virtualenv
What you've done: you created a virtual environment inside another virtual environment.
First, you created a virtual environment with "venv", and then, inside it, you created a virtual environment with "conda".
In fact, "base" is the default virtual environment of conda:
when you install Anaconda (or miniconda), your python installation is always, by default, in a virtual environement, called "base".
In other words, when you install Anaconda (or miniconda), by default, a virtual environment, called "base", is created.
In my opinion, using a conda virtual environment is enough, no need of "venv".
I would say even more, using a conda virtual environment inside a "venv" virtual environment is too much.
You should uninstall your "env" virtual environment, and after that, just install Anaconda (or miniconda).
You can create other virtual environments than "base" with conda.
Check https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html for more details.

Related

Pip installs packages in pyenv root even when virtual environment is activated

I try to install a package with pip in a virtual environement previously created with venv. The Python version is managed through pyenv. I don't want to use pyenv to create the virtual environment.
The project is set up this way. To the project empty directory, I added a .python-version containing the version 3.8.2. Then I created my virtual environement using python -m venv .venv. Then I activated the environement using source .venv/bin/activate. Now the command line starts with a (.env). However, when I try to install some package with pip install some-package, the package ends up in {pyen_home}/versions/3.8.2/lib/python3.8/site-packages, instead of the virtual environment.
What's irritating is that I'm almost certain that I did manage to install package in the virtual environment that way before, but not anymore, so I don't see what I'm missing.
Content of your .python-version should be .venv.
As far as I know you should not create this file by yourself. It generated when you run pyenv local .venv. And venv activates automatically.
Also proper way to create virtual environment is pyenv virtualenv {python-version} {venv-name}. Read the docs carefully.

Windows CMD: "This Python interpreter is in a conda environment, but the environment has not been activated"

I am trying to run Anaconda Python in a command prompt. However, when I do, I get:
This Python interpreter is in a conda environment, but the environment
has not been activated. Libraries may fail to load. To activate this
environment please see https://conda.io/activation
I have tried to run
conda base activate
and
<path to Anaconda>\Scripts\activate base
from within the Windows command prompt and it does nothing. How can I fix this?
You should have a look at the Anaconda documentation. If you want to activate the "base" environment, you should just type:
conda activate
However, it is usually better to create a virtual environment first, like this:
conda create -n myTestEnv
And in order to activate it, now write:
conda activate myTestEnv
To check the environments that you currently have on your machine you can type conda env list in the Terminal.

How do you manage virtual environments made by PyCharm after installing Conda?

I installed Anaconda last night. Previously, I was using pip and the virtual environment GUI in PyCharm to setup and manage my virtual environments.
Now I know I can create a new environment in my project structure by navigating to the project then using
conda create --prefix ./myenv mypackage1 mypackag2
That's really cool. But how do I deal with the fact that PyCharm has already created a virtual environment using virtualenv? And for older projects that I'm still working on, how do I transition from using conda to manage those project environments (also made by PyCharm and populated with pip)?
For example:
How do I use conda to install hub inside a pre-existing non-conda
environment?
How do I even call conda from an activated non-conda environment,
since when I try it doesn't register conda's existence?
Note: PyCharm just calls virtualenv to create a virtual environment. This question is not about PyCharm.

Is there a way to list all python virtual environments created using the venv module?

Conda allows me to list all virtual environments as shown here. The commands are:
conda info --envs OR conda env list
I want to do that using pip. Does pip have any option to list all virtual environments created by me ? I have created a virtual environment on my desktop but I cannot figure out a way to list it along with the base environment.
No, not in an easy way.
Python virtual environments created with venv, virtualenv and most other python-only virtual environments can be stored anywhere on the disk. And AFAIK they are not indexed, they are truly isolated (after all you can just remove venv directory and be done with it, you don't need to do anything special). They are also unmanaged by an environment manager. So that would require entire disk scan. Which potentially can be done (you can search for all Python executables for example) but is rather painful.
It works with miniconda because miniconda manages other packages and files that it installs, so it places venvs in concrete path, e.g. /home/username/miniconda/envs/.
One indirect way is to run below command and see the directories where the python executable resides. Some of the paths will be python virtual environments and you can verify it by listing the files in those paths.
$ whereis python
You can use conda to create and manage venv and virtualenv environments and other packages installed using pip.
First create a conda environment with CONDA AND PIP installed into it, e.g.,
conda create --name core --channel conda-forge python=3.9 conda pip
Here I created the conda environment named "core" and installed Python 3.9, conda, and pip into it. So now the 'core' conda environment functions like an administrative environment shell. By installing conda into the conda environment, conda will track packages installed by pip into that environment. You must use "pip install" INSIDE this new conda environment, so conda will index and track those pip package installations. However, conda will still not index and centrally manage the venv environments, like it does for its own conda environments.
Here is a very good detailed guide that explains how and why to use conda and pip for virtual environments. It covers the important aspect of using conda and pip together.
Why You Need Python Environments and How to Manage Them with Conda
Create virtual environments for python with conda

Cannot use virtual environment created within Pycharm from outside of the IDE

I am running Pycharm 2019.3 on Linux Ubuntu 18.04 LTS. I have created a virtual environment for my Pycharm project from within the IDE. I am able to access the packages from the virtual environment when I run programs from within the IDE. However, if I activate the virtual environment created from within Pycharm from the terminal (using source venv/bin/activate, resulting in the (venv) prefix on the command line), the packages I added to the virtual environment can no longer be found. Why is this? The (venv) prefix makes me think that I have successfully activated the virtual environment. What can I do to use this virtual environment outside of Pycharm?
I think this is because pycharm has created a virtual environment in a different location. I think you can check both the locations with echo $VIRTUAL_ENV and check if they are the same.

Categories