Where does venv take pip from? - python

When I run pip --version I get pip 20.2.4, but when I create a new venv such as
python -m venv venv
venv\Scripts\activate
pip --version
I suddenly get version pip 19.2.3 from the venv. I tried to search all my drives for older pip which might get used for creation of pip inside venv but I only found 20.2.4 versions.
Where does python -m venv take source for creating pip file in my venv?
NOTE: I am running on Windows as you can see by the way I active the venv.

Related

Python virtual environment not installing packages in correct directory (python)

I have set up a virtual environment correctly have activated it and when I do "which python" it tells me the correct directory, but when I do pip install in the venv it installs the package in the default directory of the mac and not in my venv. I have tried using pycharm and installing packages with that, but it happens the same thing.
Edit:
Following I will do a walkthru of my steps, first I did python3 -m venv /path/to/new/virtual/environment, then I did source env/bin/activate, then I did which python and I got the intended directory, afterwards I did pip3 install numpy and I saw the installation process, then I did pip list and numpy was not there, I checked the directory manually and it still was not there. I retried all the same thing with pycharm with the same results.
Follow these steps in order -
In your current working directory :
python3 -m venv venv
source venv/bin/activate
(venv) pip3 install library_name
To check if libraries were properly installed or not :
(venv) pip3 freeze
and check if your installed libraries is displayed on the terminal screen or not.
To deactivate the virtual environment :
(venv) deactivate
This is due to permission issues
Run this command to change permission of your virtual environment folder
sudo chmod -R 777 venv
After activating your virtual environment
source venv/bin/activate
Then try to install Django by either Python3 or Python2
pip3 install django or pip install django
Then check the list of installed packages by either Python3 or Python2
pip3 list or pip list
NOTE: If you run sudo pip3 install django or pip install in virtual environment that is not writeable by default it will be installed outside of virtual environment
If you run pip3 install django or pip install in virtual environment that is not writtable by default it will be installed outside of virtual environment you will get OS Persmission Error
I have used pip3 and pip because you can install packages by either Python3 or Python2, thing to remember if you created virtual environment by Python3, use pip3 and if you created by using Python2 use pip.

How to create venv

I have been using my python v3.9 in my virtual environment, it contains all the packages and scripts i have been using for so long. But, now with the release of python v 3.10 it installed itself globally though I wanted it to be installed in same venv I was using for python v3.9 So, if anyone could help me with how can I install python v3.10 in the same venv of my v3.9 . My IDE is PyCharm
You can't.
A virtual environment includes a copy of Python binaries. It can't be both 3.9 and 3.10.
You must create a 3.10 venv.
What you can do is get a list of all libs in your 3.9 venv and install them in your 3.10 venv.
# In 3.9 venv
pip freeze > requirements.txt
# In 3.10 venv
pip install -r requirements.txt
Simply put all the dependencies of your python 3.9 (venv) in requirements.txt file
pip freeze > requirements.txt
Create a new folder then move that file inside the newly created folder
then execute the following code, it will create a new virtual environment with python 3.10
python -m venv newenv
activate the newly created environment by
source newenv/bin/activate
then install the required dependencies by
pip install -r requirements.txt
Note: If your OS do not have 'venv' module then simply install it by using
pip install venv

Install pandas in debian10 on a different python version

I have a device with python3.7 preinstalled, in which i have installed also python3.9. I managed to change the version I am using of python3 and now the command "python3" followed by the .py file runs with python3.9.
The problem is I tried installing pandas with pip3 but it does not work (it didn't work even in the preinstalled python3.7), so I found that in debian you can install package, for example in this case pandas, using "sudo apt-get install python3-pandas" but this command keeps installing pandas in python3.7 and not in python3.9 even if now "python3" refers to python3.9.
Has anyone ever encountered this problem and has a solution?
python3.9 -m pip install pandas
Venv
You could use a virtual environment (venv) for installing dependencies.
This venv could be project specific or global.
Run python3 -m venv .venv in your project folder to create a .venv folder, which holds the venv configuration.
Run source .venv/bin/activate to activate the venv. This will link pip3 from your python 3.9 version to the pip command.
Now you can do pip install pandas to install the pandas dependency into the venv.
Conda
Another solution would be to use Anaconda or Miniconda
https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html
conda create -n name_of_my_env python
This will create a minimal environment with only Python installed in
it.
To put your self inside this environment run:
source activate name_of_my_env
On Windows the command is:
activate name_of_my_env
The final step required is to install pandas. This can be done with
the following command:
conda install pandas

In a virtualenv pip installs globally and pip3 locally

I've created a virtualenv called venv and activated it. When using which both pip and pip3 point to the venv/bin folder. Yet when trying to install packages and listing them will there is a difference. When using pip all my global packages are listed and new packages are installed globally. With pip3 the behavior is as expected containing everything within the virtual environment.
Here is a small console log:
$ virtualenv venv
$ source venv/bin/activate
(venv)$ which pip
./venv/bin/pip
(venv)$ which pip3
./venv/bin/pip3
(venv)$ pip list
SHOWS GLOBAL PACKAGES
(venv)$ pip3 list
SHOWS LOCAL PACKAGES
How do I get the correct with pip using the virtual environment?
I am using Ubuntu 18.04.4 LTS with python 3.6.1 and pip 20.0.2.

Wrong pip version for anaconda's environments. What should I do to correct the pip version when activate a env using `conda activate`?

I have 4 envs in my anaconda as listed below.
>>> conda info --envs
base /home/gph/anaconda3
py36_torch0.4 * /home/gph/anaconda3/envs/py36_torch0.4
py37_torch1.1 /home/gph/anaconda3/envs/py37_torch1.1
python3.6 /home/gph/anaconda3/envs/python3.6
In both py36_torch0.4 and py37_torch1.1, I did some test.
>>>(py36_torch0.4) gph#gph-1050Ti:~ $ whereis pip
pip: /home/gph/.local/bin/pip3.5
/home/gph/.local/bin/pip
/home/gph/anaconda3/envs/py36_torch0.4/bin/pip3.6
/home/gph/anaconda3/envs/py36_torch0.4/bin/pip
(py36_torch0.4) gph#gph-1050Ti:~ $ pip -V
pip 19.1.1 from /home/gph/.local/lib/python3.5/site-packages/pip (python 3.5)
(py37_torch1.1) gph#gph-1050Ti:~ $ whereis pip
pip: /home/gph/.local/bin/pip3.5
/home/gph/.local/bin/pip
/home/gph/anaconda3/envs/py37_torch1.1/bin/pip
(py37_torch1.1) gph#gph-1050Ti:~ $ pip -V
pip 19.1.1 from /home/gph/.local/lib/python3.5/site-packages/pip (python 3.5)
We can see that for each env_name we have envs/env_name/bin/pip, but the output of pip -V is always /home/gph/.local/lib/python3.5/site-packages/pip (python 3.5).
What is wrong? What should I do to make the pip version right when activate a specific env?
I have tried unset PYTHONPATH as told in this question: Wrong pip in conda env. But it is no use.
Use pip as a module called by the Python interpreter of your env.
python -m pip install some_package where python is the one active on your env, for example /home/gph/anaconda3/envs/python3.6/bin/python.
You've installed pip outside of the conda environments, directly into your home directory. This takes precedence over everything that's installed in conda.
If you want to benefit from isolation of conda environments, you cannot install packages on the user level. This will get rid of all of them (but maybe also some more):
rm -rf /home/gph/.local
You might want to have a look at what else is installed in /home/gph/.local before actually removing it.

Categories

Resources