Conda Virtual Environment Packages - python

Lately I've been using conda instead of virtualenv and I think I like it better, except when I install packages I don't think they are only installed into my virtual environment.
If I run pip freeze within my virtual environment I get the list of packages installed, no surprise there. However, when I deactivate the virtual environment and run pip freeze again, I get the same list. Is my environment not separated from my base distro? Plus, how do I create a clean environment if my packages are shared across all environments?
Note: I noticed this after installing packages with pip using pip setup.py install within a package. However, according to the docs you can use pip in a conda virtual environment.

Related

Installing packages with pip

I have been advised using pip in a Anaconda virtual environment is bad. But some packages are not on conda or on conda forge.
When I run
conda activate virtualenv
where pip
I get two paths, one which is outside the environment
C:\Anaconda\virtualenv\Scripts\pip.exe
C:\Anaconda\Scripts\pip.exe
How do I fix it, so that when I do pip install package it only installs in the virtual environment?
The command line should use the first pip it finds, which in your case is the one in the virtual environment. This pip will only install packages in your environment. You can check which one is running with pip --version.

Packages installed in my virtual environment arent found unless I install them using cmd prompt in windows

I am currently working on a python project using Visual Studio Code and Conda. When I try to install a package in the virtual environment it does not work (details and steps below). But if I use my computers command prompt and install that way, it does work in the virtual environment.
My steps are as follows:
I created a virtual environment as so:
conda create -n envname
Activated the environment:
conda activate envname
Tried to install a package (i tried to use conda install but package wasnt found):
pip install packagename
Then write it into my code like so:
import packagename
Which throws me this error:
ModuleNotFoundError: No module named 'packagename'
I did try the solution from this page and tried installing the package this way:
C:\Users\myname\anaconda3\envs\envname\Scripts\pip install packagename
but it returns this:
Requirement already satisfied: packagename in c:\users\name\anaconda3\envs\envname\lib\site-packages
when i type:
conda list
The package I installed is included, as well as pip. However, the package I installed shows the version # in one column, then only "pypi_0 pypi" in the last column. not sure if that means something is wrong or not.
packagename 3.41 pypi_0 pypi
As mentioned, if I type this in my computers command prompt (separate from VSC and my virtual environment):
pip install packagename
My virtual environment will pick it up as being installed.
I feel all mixed up lol, like some path somewhere isnt right but I cant figure out what
Any ideas?
No Need to explicitly activate the venv
Try using following commands.
Step 1: Create virtual env
python -m venv envname
Step 2: Switch to this newly created virtual env
Windows
.\envname\Scripts\activate
Unix
source envname/bin/activate
Step 3: install packages in the virtual env directory
pip install <packagename>

Does a package also gets installed in conda environment if I use pip to install packages not included in Anaconda?

I am currently working on a Django project and want to install an additional Django (app) package called "tinymce". I've installed Anaconda just recently and I'm not used to commands related to conda. I often used to pip install packages from the command prompt. But now I'm really confused if I really should just use the same pip install or use some other conda commands (if there are any) to install that package. Also, does that affect anything, if I am to frequently use that packages?
FYI: I'm on Windows & using Python 3.7 using the recent Anaconda release.
Before you start installing packages, you should decide on how you want to manage your packages for different projects. I'd recommend that you create a dedicated conda environment for each project. Then you have to activate the respective environment whenever you want to work on a project. But packages installed for one project don't interfere with those for another. It helps to install Miniconda rather than Anaconda, because that keeps the conda base environment clean.
You write that you're used to calling pip install, but you don't mention Python virtual envs nor conda environments. That sounds as if you're typically installing packages globally on your machine. Sooner or later, that's going to create a mess.
If you decide to use conda environments, you have to remember to always activate the environment for your project before installing packages for that project. Then both pip install and conda install will put packages into that environment. When I have a choice, I prefer to install packages with conda from its default channels. conda has better dependency management than pip, and conda can handle non-Python dependencies. But packages sometimes have different names in conda and pip, so it might be extra effort to translate installation instructions for pip into similar commands for conda.

Installed standalone Python 3.7 after Anaconda Python 3.7, how to access old/specific pip?

I'm relatively new to Python. I installed 3.7 as part of Anaconda package, but it wasn't working with PowerBI since it wasn't able to execute a certain batch file that started the conda venv. A workaround I read about was to install a regular Python 3.7 outside of conda and use that interpreter instead.
It successfully installed, and it was added to path, and when I do pip-list in my command prompt I see only the packages for the new pip which makes sense. How do I access the old pip for my conda python where it had all the packages?
Do you mean how do you install packages in your default Python environment outside of Conda? To achieve this you'd just run pip install to install the packages fresh. If you want the list of previously install items you can use pip list.
conda activate myoldenv
pip freeze > requirements.txt
You can then open requirements.txt to see which packages that you want.
However if you want PBI to work well with Python the cleanest way in my opinion is to setup within a new conda env. The point of Conda is to have different envs for different use cases.
conda create --name VisualisationPBI python=3.7
conda activate VisualisationPBI
pip install seaborn #installs seaborn and dependencies including numpy, pandas and mpl
In PBI options set your Python home directory to "Other" then select your conda directory, probably:
C:\Users\YOURNAME.conda\envs\VisualisationPBI
Conda works with PBI, but you need to Pip install the key packages in the library, instead of conda install. As here: https://community.powerbi.com/t5/Desktop/Power-BI-Python-with-Anaconda-missing-dependency/td-p/665102

Conda: packages are already installed by pip but not shown in conda list

I use pip install packages in a conda environment.
pip install pygame
Requirement already satisfied: pygame in ./anaconda3/lib/python3.6/site-packages (1.9.4)
where the current directory is /Users/aptx4869. However, when I type conda list, there is nothing in the current environment. What's wrong with it? Here's the directory where the environment is at
/Users/aptx4869/anaconda3/envs/rl
Update
I delete the pygame in the root environment and run pip install pygame in the rl conda environment, I receive another message. But pygame still doesn't show in conda list
pip install pygame
Collecting pygame
Using cached https://files.pythonhosted.org/packages/bc/19/57bf1e9c72be4f7afc1add56cc717b7f7fe8ef1b6b5fb58f031a06401d0f/pygame-1.9.4-cp36-cp36m-macosx_10_11_intel.whl
Installing collected packages: pygame
Successfully installed pygame-1.9.4
(rl)
Notice (rl) in the end, this pip command still installs pygame in the root environment
The reason is simply because I didn't install python and pip in the dl environment, and conda implicitly uses python and pip in the root environment as I command pip install ...
I guess that you installed the pygame package into the root environment when you run pip install pygame the first time. So make sure that you have activated the environment into which you want to install packages and then use pip to install the packages. Doing this, you should see the packages in the list of conda list command. Also, you must run the conda list command in the same environment where you run pip install.
After facing the same problem, here is the solution that worked for me:
From a separate terminal (not VSCode integrated terminal), with my virtual environment active:
python -m pip install pygame
It is important not simply call pip (or pip3) directly, and it apparently mattered to not do it from VSCode integrated terminal.
I've run into this a few times with different packages, and 9 times out of 10 it's an issue wit managing multiple conda environments.
You can have multiple conda environments active at the same time. If you activate one environment from another environment, it doesn't necessarily close the first environment. So, say that you create myenv:
(base)$ conda create myenv
(base)$ conda activate myenv
(myenv)$
You work in that environment for a bit, run into some problems, and then realize that it's easier to start another environment from scratch. If you do
(myenv)$ conda create myenv2
(myenv)$ conda activate myenv2
(myenv2)$
You have activated myenv2. However, you haven't explicitly closed myenv. You can see this when you deactivate myenv2
(myenv2)$ conda deactivate
(myenv)$
I have't researched this behavior too deeply, but I know that it can create issues with pip installs within conda. Try deactivating your conda environments to base and then activating only the environment of interest. It fixed the problem for me, at least.

Categories

Resources