Pip installs to anaconda directory instead python's directory (Windows) - python

Whenever I try to install package with pip (using wheel or just regular pip install numpy ->e.g), pip installs new package to location where Anaconda holds its site-packages. How do I remove that? That started happening since I installed Anaconda which I use for some tasks as python interpreter, but now I need my regular python installation.

If you have Python 3 installed but you see that which pip points to your Anaconda installation, try using pip3 instead - if it is available then you will see that which pip3 points to your Pythons installation path instead of your Anaconda path. Same with which python3.

Instead of just writing pip instal ... in the command line, which apparently points to your Anaconda installation, you can navigate (using the cd command) to your Python installation and invoke the pip.exe file located somewhere there.
I guess you could try renaming one of pip.exe files (the one in Anaconda or the one in Python) to something else (e.g. pipanadonda.exe), and then you will be able to call them separately from the command line.

Related

How do modules installation work in Python?

[On a mac]
I know I can get packages doing pip install etc.
But I'm not entirely sure how all this works.
Does it matter which folder my terminal is in when I write this command?
What happens if I write it in a specific folder?
Does it matter if I do pip/pip3?
I'm doing a project, which had a requirements file.
So I went to the folder the requirements txt was in and did pip install requirements, but there was a specific tensorflow version, which only works for python 3.7. So I did """python3.7 -m pip install requirements""" and it worked (i'm not sure why). Then I got jupyter with brew and ran a notebook which used one of the modules in the requirements file, but it says there is no such module.
I suspect packages are linked to specific versions of python and I need to be running that version of python with my notebook, but I'm really not sure how. Is there some better way to be setting up my environment than just blindley pip installing stuff in random folders?
I'm sorry if this is not a well formed question, I will fix it if you let me know how.
Yes, there is. Setup an virtual environment.
pip install virtualenv #installs the library
virtualenv mypython #creates the environment
source mypython/bin/activate #activates the environment
Now, install your requirements through pip.
Afterwards, when your work is finished.
Just type deactivate to come out of the virtual environment.
There may be a difference between pip and pip3, depending on what you have installed on your system. pip is likely the pip used for python2 while pip3 is used for python3.
The easiest way to tell is to simply execute python and see what version starts. python will run typically run the older version 2.x python and python3 is required to run python version 3.x. If you install into the python2 environment (using pip install or python -m pip install the libraries will be available to the python version that runs when you execute python. To install them into a python3 environment, use pip3 or python3 -m pip install.
Basically, pip is writing module components into a library path, where import <module> can find them. To do this for ALL users, use python3 or pip3 from the command line. To test it out, or use it on an individual basis, use a virtual environment as #Abhishek Verma said.

why "pip install" doesn't work but "python -m pip install" works?

I've recently reinstalled windows on my workstation so I needed to reinstall python and pip, before the reinstallation, the command pip install whatever was working properly.
But then after the reinstallation of Windows 10 when I type that command it won't return anything, not even an error, I've searched other solution and I found that this command python -m pip install whatever works fine.
What is changed?
How can I let the first command work again?
This is a PATH problem. On a standard Python3 installation on Windows, python.exe and pip.exe are in different folders. Let us say that Python is installed just under TOP, then:
python.exe (and pythonw.exe) are in TOP\Python3x (x being minor version such as Python37)
pip.exe is in TOP\Python3x\Scripts
py.exe is in WINDOWSDIR (normally C:\Windows)
That is the reason why:
py -m pip install ... always works on Windows (provided pip is installed)
python -m pip install ... requires ...\Python3x to be in the PATH
pip install ... requires ...\Python3x\Scripts to be in the PATH
As Bensalem has said, you need to add pip to your variable path in order to be able to use it without calling the script with python (which is in your path).
The -m refers to the passable argument to python;
-m mod : run library module as a script (terminates option list)
pip is a python script, and because you have not added to path variable the link to the "Scripts" folder;
this "Scripts" folder is situated on "C:location where python is installed\Scripts";
ex. for me : "C:\Python36\Scripts"; so i type on cmd prompt to set the path variable:
set PATH=%PATH%;C:\Python36\Scripts
you can see the link: [https://www.computerhope.com/issues/ch000549.htm]
you should add pip to ur environment variable(path windows), try this

How to move all modules to new version of Python (from 3.6 to 3.7)

I just upgraded to python 3.7 and I realized that all my modules stuck with the previous version. Even Django is not recognised anymore. How can I do to transfer everything to the new version? I am a little lost right now, don't even know where the new version has been installed.
Edit:
When I do $ which python3.6 the terminal tells me it doesn't exist, but I have a python3.6 directory in /usr/local/lib/, where all modules are installed.
In the same directory /usr/local/lib/ I also have a python3.7 directory with some modules installed but many are missing. However when I search for the file python3.7 in my finder it doesn't appear. when I do $ which python3.7 the path is /usr/local/bin so not the same path as the directory.
Anyone sees what happened and knows how I can transfer all modules to python3.7?
Even if the old python version has been removed, it is possible to use the pip of the current python version with the --path option to list all the modules installed in the previous version.
For example, migrating all my user installed python modules from 3.7 to 3.8
pip freeze --path ~/.local/lib/python3.7/site-packages > requirements.txt
pip install --user -r requirements.txt
Incidentally, I always use pip install with --user and leave the system wide installations to the package manager of my linux distro.
It is safer to re-install all packages due to possible compatibility issues:
pip3.6 list | awk '{print $1}' | xargs -I{} pip3.7 install {}
in older version of Python --run the command
pip freeze > requirements.txt
download and install newer version on python.. change the PATH variable to the new version
and run the command
pip install -r requirements.txt
I'm not sure about all modules...but if you want to install a module specifically in python3.7, try this:
python3.7 -m pip install *module_name*
In some cases, we don't have the opportunity to pip freeze in old version--because I've already updated and old version have been purged! There are some measures I've taken to recover some of the packages but I'm NOT sure every package would work with this fix.(e.g. the packages built with wheels)
mv /your/path/to/python3.{6,7}/site-packages/
If the case is packages installed outside venv (in /usr/local/ or ~/.local), reinstall pip with get-pip.py, just to be safe.
If you are recovering a virtualenv. Activate your virtualenv and use my script
Most of your packages should work by now. If anything malfunctions, pip reinstall would works. If you still want it 100% works, pip freeze now.😉
I have an alternative
(Not sure if works outside Windows 10)
I'm currently migrating from 3.7 to 3.8 and the way I found to re-install my previous libraries was by using a script I had that updates all packages via pip install. (Assuming you installed your new Python version as your main version) This checks for all the packages I had and updates/install them in the new Python version.
Note: I prefer to run the script from the command line
Use the file explorer to go to the folder where you have the script;
Click on the path box, write "cmd" and press enter to open a command line from the folder where you are;
Write "python name_of_your_script.py" and press enter to run the command.
The script (adapted from this solution):
import pkg_resources
from subprocess import call
packages = [dist.project_name for dist in pkg_resources.working_set]
[call("pip install " + name + " --upgrade") for name in packages]
I faced a similar problem, now that I upgraded from python 3.7 to python 3.8 (new)
I installed Python 3.8, but the system kept the python37 subfolder with the already installed packages(...\Python37-32\Lib\site-packages) even with the Pyhton38 subfolder created, with the new python.exe.
Usually, it's possible to keep on using the old libraries on your new Python version, because the existent libraries installation folder are already registered in your local computer system path (*).
Even though, I've had problems to use some libraries (some worked in Jupyter Notebook but not in Spyder). I tried the alternatives others proposed to migrate libraries but it did not worked (maybe I did not
So I used brutal force solution.. Not elegant at all, but it worked:
remove the OLD python version folders from the system path or even remove the folder itself for good..
Folders: C:\Users\USERNAME\AppData\Roaming\Python\Python37
C:\Users\USERNAME\AppData\Local\Programs\Python\Python37
Reinstall the packages you need, preferably via Anaconda prompt.
python -mpip install library_name
OR
pip install --user --force-reinstall package_name
OR
pip install --user --force-reinstall package_name == specify_package_version
The libraries will be installed at c:\users\USERNAME\anaconda3\lib\site-packages and recognized by your new python version.
(*) to add the folder to the PATH: System properties --> environment variables --> click "Path"--> edit --> add folder name)

How to pip install python packages when anaconda is installed?

I installed anaconda and now I can't use pip to install packages on my version 3.6 of python, instead it installs them on anaconda. If I type pip -V I get:
pip 9.0.1 from /anaconda/lib/python3.6/site-packages (python 3.6)
so how do I make it so that it shows:
/Library/Frameworks/Python.framework/Versions/3.6/
instead?.
Or am I missing something?
Well, anaconda comes with its own python directory, which replaces your default python and thus renders its pip useless.
You need to edit .bashrc (or .zshrc in case you are using zsh). There you need to change the path to your original python directory. First of all delete anaconda related export PATH and put in:
export PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:$PATH"
If you are using Debian based system. You can also try update-alternatives. This is very powerful (yet easy to use) tool, which allows you to change current default python version without manually updating .bashrc.
How to use update-alternatives can be found here: https://linuxconfig.org/how-to-change-from-default-to-alternative-python-version-on-debian-linux#h2-change-python-version-system-wide
I had to work this out myself awhile back:
On Windows 10, I entered command prompt (iecmd) and used:
python -m pip install --target=C:\Users\myusername\AppData\Local\Programs\Python\Python36-32\
modulename
I assume if you can find the folder your python 3.6 then you'll be able to use:
python -m pop install --target=whereever\your\python\is themoduleyouwant
Note that the space between the path and the module name is necessary.

How to configure pip to install packages for python 2.x and python 3.x separately

I am running Windows x64 bit.
I downloaded the Pyro4 package via pip install Pyro4. It downloaded the packages successfully and they are all present in my "C:\Python34\Scripts" folder as I've kept Python3.4 as default.
Now when I went to the that "C:\Python27\Scripts" folder, the Pyro4 package is not to be found. This is as expected, but I would like to work on both Python 2.7 and 3.4 as Pyro4 is compatible in both.
How do I change my pip command to download the package to Python 2.7's installation scripts directory?
First make a new environment variable:
Go to your system properties
Under Advanced tab click Environment Variables...
Under System variables section click New...
Variable name: (whatever you can remember for example p27s)
Variable value: your python 2.7 scripts folder ("C:\Python27\Scripts\")
From now on whenever you want to install a package for python 2.7 you can do it this way: %your_variable_name%pip install package_name
For example: C:>%p27s%pip install Pyro4
This way you can install any package for python 2.7 and use default pip for python 3.4
You will need to go to your environment variables in the control panel and change the path from C:\Python34\Scripts to C:\Python27\Scripts. After that change, when you type 'python' in the command prompt it will be using Python 2.7. Next, install pip like you initially did.
Not exactly what you're asking, but you could check out Anaconda which allows you switch python environments really easily.
https://www.continuum.io/downloads
Edit
Just to clarify
if you were to use this method, you can have named environments say python27 and python34
then you can just:
activate python27
pip install module
activate python34
pip install module
and it will install them to both environments.

Categories

Resources