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.
Related
I am trying to create Conda enviroment via PyCharm. Upon creation of env, conda is trying to install some new packages. One of them is considered vulnerable by my corporation, so it's installation is blocked. After that, creation of env just breaks and I am left with no env whatsoever. How do I exclude banned package or configure packages that are installed upon creating env?
I am working on a python project in Pycharm. Lately I have created a setup.py in order for collaborators to be able to install and test the project on their systems. To test that everything is working fine, I have also installed it in my conda environment, which is the same as used by the interpreter in my Pycharm project. Now when I import my package in Pycharm it gets imported from the conda environment and not from my Pycharm project. Is there a way to tell Pycharm to look first in the Pycharm project subdirectories and then in the conda environment? Or do I need to uninstall my package from the conda environment that I use in Pycharm?
The proper way to go is to first create a virtual environment and then install your package in editable mode along with its required dependencies, as specified in setup.cfg.
Step 1: Activate your virtual environment
source /path/to/your/venv/bin/activate
or
conda activate vevnName
Step 2: Install your package in editable mode
pip3 install -e .
This command is going to install your package and link it to the original location (i.e the one that probably you want to be editing in PyCharm) so that any change being made is also replicated on your venv.
You can now open your Python application on PyCharm, from its original location and any changes you make in your IDE will be reflected directly on your virtual environment.
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.
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
I have set up a python virtual environment with pyenv on Linux Now I would like to create a Django project in PyDev with one of these virtual environments. However, I cannot figure out how to locate my virtual environment, since running which python in the virtual environment only gives me the generic /home/rbu/.pyenv/shims/python.
First find your virtualenv directory with
pyenv prefix <venv-name>
The python executable of the virtualenv should be <path>/<to>/<venv>/bin/python.
Now set up a new interpreter in Eclipse Preferences>PyDev>Interpreters>Python Interpreter using the location of the executable and a adequate name.
After that you can start a new Django Project via File>New>Project>Pydev>PyDev Django Project. Choose your predefined interpreter.
The Django Project should now work inside the virtualenv. For installing new packages it is probably easiest to just activate the virtualenv in the terminal with
pyenv activate <venv-name> and pip install the package.