Install package locally - python

I use python's pip to install packages. Now I want to install scipy, which is already installed on the system, but an old version and on a part of the system where I don't have access to. If I try
pip install scipy
pip rightfully tells me that the package is already installed. If I do
pip install scipy --upgrade
pip tries to upgrade the package but I don't have the access rights to do that.
How can I tell pip to install the package local to my user and to ignore the other scipy package?

I think the best way for avoid override packages it's using a virtual environment. Python has it's own virtual environment and you could install it by:
Python 2.7
> sudo apt-get install python-virtualenv
Python 3
> sudo apt-get install virtualenv
With modern python versions, virtualenv is usually included. Once installed, you could generate a virtual enviroment typing:
> virtualenv venv
This would create a folder in the current directory named venv (you could name it whatever you want). In this package the libraries will be installed.
So, it's time to activate the virtual environment
> source venv/bin/activate
You could verify the environment has been activated by checking the prompt changes. If it happens, all the packages installed using pip will be installed locally.
(venv)> pip install scipy
You could check this website for more info.
Don't forget that you eventually have to clear your $PYTHONPATH variable, in order for it to not pick up other packages.

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.

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.

Where to install pip packages inside my Conda environment?

As I understand it, if I use pip install ___, that package will go to my global version of python. If I change directory to the within my Conda environment then that package will be isolated within the environment. Is this correct?
I have searched to try and find where to put the pip packages (within my Conda environment). It used to be that you would install the pip packages in /Anaconda3/envs/venv_name/bin/. It appears the bin folder is now located within the Library folder, like this: /Anaconda3/envs/venv_name/Library/bin. Is the bin folder still the recommended place to put the packages installed by pip?
In other words should I be placing the pip installed packages here: /Anaconda3/envs/venv_name/Library/bin ?
No Specification Needed
Fortunately, one need not manually specify where to install the packages. Instead, if one uses the pip associated with the environment, packages will install to the site-packages directory of environment's python by default.
Example
> conda activate venv_name
# check that you are using the right pip
> which pip
/Anaconda3/envs/venv_name/bin/pip # should be something like this
> pip install <package name>
This will install packages into /Anaconda3/envs/venv_name/lib/python3.7/site-packages/, or whatever Python version you have installed for the environment.
⛔️ Important Note: There are some flags for pip install that change this behavior, most notably the --user flag. Conda users are strongly discouraged from using this flag because it installs packages at a user-level, leading to packages being visible to other environments with matching Python versions (major+minor).
Caution: Mixing PyPI and Conda Packages
Be aware that (as #WilliamDIrons pointed out), it is usually preferable to use conda install -n venv_name <package name> instead of pip. The common practice is to only use pip in a Conda environment when the package is not available through a Conda repository. It is strongly recommended to read and follow the best practices found in the "Using pip in an environment" documentation.

Python If I should use pip/pip3 to install/update packages when anaconda is my python organizer?

I was informed that if I have installed anaconda to organize python then I would better install using:
conda install mypackage
rather than
pip3 install mypackage
Is that true? if that is true, can anyone tell some reason for that? version inconsistent or hard to maintain?
You can install your packages with both conda and pip, all of them would work well. The only difference is that conda is Anaconda's package manager, while pip is Python package manager, so there could be some version incompabilities between the packages, installed from different packages into one virtual environment.
Actually there are some difference here:
conda install will install package in your venv environment when you are install under some environment.which may be some thing like: d:/.../venv
while
pip install will install package in some system folder, in my computer is like c:/users/.../
you can definitely change the order of the path in your sys.path to decide which version of package you can use, if you have more than one version installed(if you install numpy
using both conda install and pip install then you may get two versions in two different folders)
There may be some way to put the installed package from pip also in venv folder, I am trying to find it.

Bypass confirmation prompt for pip uninstall

I'm trying to uninstall all django packages in my superuser environment to ensure that all my webapp dependencies are installed to my virtualenv.
sudo su
sudo pip freeze | grep -E '^django-' | xargs pip -q uninstall
But pip wants to confirm every package uninstall, and there doesn't seem to be a -y option for pip. Is there a better way to uninstall a batch of python modules? Is rm -rf .../site-packages/ a proper way to go? Is there an easy_install alternative?
Alternatively, would it be better to force pip to install all dependencies to the virtualenv rather than relying on the system python modules to meet those dependencies, e.g. pip --upgrade install, but forcing even equally old versions to be installed to override any system modules. I tried activating my virtualenv and then pip install --upgrade -r requirements.txt and that does seem to install the dependencies, even those existing in my system path, but I can't be sure if that's because my system modules were old. And man pip doesn't seem to guarantee this behavior (i.e. installing the same version of a package that already exists in the system site-packages).
starting with pip version 7.1.2 you can run pip uninstall -y <python package(s)>
pip uninstall -y package1 package2 package3
or from file
pip uninstall -y -r requirements.txt
Pip does NOT include a --yes option (as of pip version 1.3.1).
WORKAROUND: pipe yes to it!
$ sudo ls # enter pw so not prompted again
$ /usr/bin/yes | sudo pip uninstall pymongo
If you want to uninstall every package from requirements.txt,
pip uninstall -y -r requirements.txt
on www.saturncloud.io, Jupiter notebooks one can use like this:
!yes | pip uninstall tensorflow
!yes | pip uninstall gast
!yes | pip uninstall tensorflow-probability
Alternatively, would it be better to force pip to install all dependencies to the virtualenv rather than relying on the system python modules to meet those dependencies,
Yes. Don't mess too much with the inbuilt system installed packages. Many of the system packages, particularly in OS X (even the debian and the derived varieties) depend too much on them.
pip --upgrade install, but forcing even equally old versions to be installed to override any system modules.
It should not be a big deal if there are a few more packages installed within the venv that are already there in the system package, particularly if they are of different version. Thats the whole point of virtualenv.
I tried activating my virtualenv and then pip install --upgrade -r requirements.txt and that does seem to install the dependencies, even those existing in my system path, but I can't be sure if that's because my system modules were old. And man pip doesn't seem to guarantee this behavior (i.e. installing the same version of a package that already exists in the system site-packages).
No, it doesn't install the packages already there in the main installation unless you have used the --no-site-packages flag to create it, or the required and present versions are different..
Lakshman Prasad was right, pip --upgrade and/or virtualenv --no-site-packages is the way to go. Uninstalling the system-wide python modules is bad.
The --upgrade option to pip does install required modules in the virtual env, even if they already exist in the system environment, and even if the required version or latest available version is the same as the system version.
pip --upgrade install
And, using the --no-site-packages option when creating the virtual environment ensures that missing dependencies can't possibly be masked by the presence of missing modules in the system path. This helps expose problems during migration of a module from one package to another, e.g. pinax.apps.groups -> django-groups, especially when the problem is with load templatetags statements in django which search all available modules for templatetags directories and the tag definitions within.
pip install -U xxxx
can bypass confirm

Categories

Resources