Installing packages with pip - python

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.

Related

Installing packages in python installed using home-brew

Hey everyone I installed Python3.9 on my Mac using homebrew package manager but now I do not know how to install packages to it for use
Can anyone please tell me, thanks !
You should first do some research on the Python virtual environment, but the final answer to your question is to use pip install for installing Python packages. Be aware that there are other options out there, but pip is the most prevalent.
When you installed python, it has pip installed by default. pip comes with python. You can check pip version by
pip --version
OR
pip3 --version
Now, in order to install any other package, you can install by
pip install <package-name>
It would be better if you install a virtual environment, and install all other packages inside the virtual environment so that you can install packages according to your project requirements and with different versions.
To install virtual environment, do
pip install virtualenv
Once the virtual environment is installed, you can create your virtualenv according to your project requirement by:
virtualenv -p python3 venv
Here venv is your virtualenv name. To activate it,
source venv/bin/actiavte
Now, you can install all your required packages inside this virtualenv by pip3 install <package-name>. This will keep it separated from your system environment.

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>

conda equivalent of pip install

If I have a directory with setup.py, in pip, I can pip install . in the directory to install the package.
What if I am using conda?
conda install . makes conda to find a package named dot.
conda packages are a different structure than standard python packaging. As a result, the official, recommended and best-practice approach is to use conda to install pip within an activated conda environment, and use that to install standard packages:
conda install pip
NOTE: You want to use conda packages whenever they're available, as they have more features within a conda environment than non-conda packages.
conda install pip will install pip within the currently activated conda environment, and will ensure that it is integrated with conda so that, for example, conda list, will include any packages installed with pip.
NOTE: Commands like conda update will ignore pip installed packages, as it only checks conda channels for available updates, so they still need to be updated using pip. See this Question/Answer discussion:
Does conda update packages from pypi installed using pip install?
NOTE: See #kalefranz comment below regarding conda 4.6 experimental handling of packages.
If you're interested in creating your own conda package(s), take a look at this question/1st answer for a great run-down:
How to install my own python module (package) via conda and watch its changes
If you simply wish to install non-conda packages, using pip is the correct, and expected, way to go.
You can use pip install from within conda environment.
Just activate your environment using:
$ conda activate myenvironment
and use pip install . to install your package in environment's directory.
EDIT: As pointed by Chris Larson in another answert, you should install pip inside the environment using
$ conda install pip
in order to register packages correctly.
If I have a whl file, I can use pip install xxx.whl to install it.
From the documentation, conda install from a local file is also available, but the file should be a tarball file, i.e. .tar.bz2 files.
conda install /package-path/package-filename.tar.bz2 works. And if I have multiple tarballs, I can tar them to get a .tar file, then conda install /packages-path/packages-filename.tar installs the packages in it.

Conda Virtual Environment Packages

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.

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