Changing python pip defualt install directory - python

I followed this guide to change my default install directory:
~/.pip/pip.conf:
[global]
target=~/.python-global
So that pip does not interfere with pacman. However, I need to be able to separate python2 and python3. So is there a way to get pip2 to point to ~/.python-global/python2 and pip3 to point to ~/.python-global/python3?

Related

Default pip install to --user on Windows

I am using pip as my package manager for Python on my Windows 11 machine. I always install all my packages to --user. So, having to add --user argument every time I install a package is kind of annoying since it's my desired default installation path already!
> pip install package1 package2 package3 --user
On Ubuntu, if sudo pip is not used (which is recommended not to use it), pip install defaults to user
$ pip install package1 package2 package3
Defaulting to user installation because normal site-packages is not writeable
...
Is there any way to force pip to install packages to user by default as if --user argument is added without explicitly appending it at the end?
I always add --user --upgrade --verbose to any pip install command
You can look at the pip documentation here.
For the following to work you might have to go to File Explorer and at the top go to View and make sure you have selected hidden items from the options.
You will need to specify the default install location within a pip.ini file. Which, is usually located at %APPDATA%\local\pip\pip.ini(on Windows).
The %APPDATA% is located in C:\Users\username then go to AppData on Windows.
You may have to create the pip.ini file when you find your pip directory. Within your pip.ini you will then need to put something like:
[global]
target=C:\Users\user
user being your username for your Windows machine.

Different custom install locations for pip2 and pip3

When using pip with the --user flag, the default installation location is ~/.local/lib/pythonX.Y/site-packages, where X.Y specifiy the version of python. This allows for separation of packages installed using pip2 from those installed via pip3.
However, when using a pip.conf file to specify a target installation directory, I've only seen a global setting such as this:
[global]
target=/data/user/pip
This works, but doesn't separate packages installed by pip2 from those installed via pip3 which can cause issues. Is there a way to specify different locations for packages installed via pip2 and those installed via pip3?
Unfortunately, there's no possibility to handle version-specific stuff in the pip config. The current decision about this is:
...it doesn't appear to be something we actually need.
However, the user installation target is actually configured not via --target, but via the PYTHONUSERBASE environment variable. This means that you can pass the user base from env, for example PYTHONUSERBASE=/some/dir pip install --user pkgname. If you want to persist the custom user base dir, I'd go with an alias. Example for bash: in your .bashrc/.bash_profile, add:
alias pip2='PYTHONUSERBASE=/tmp/pip2 pip2'
alias pip3='PYTHONUSERBASE=/tmp/pip3 pip3'
alias pip3.7='PYTHONUSERBASE=/tmp/pip3.7 pip3.7'
# etc
Save the file, reload with
source ~/.bashrc
or
source ~/.bash_profile
or simply open a new terminal. Now
$ pip2 install --user pkgname
will install to /tmp/pip2 etc.

Why pip3 install in python2 sitepackages

First I use
Python 3.6.5
Python 2.7.14
and mac.
In my case, I just download module like numpy(for example, and other's same) when i use pip3 it said like..
and pip is same.
but when I use it, in python3
In python2, It working well...
How can I fix it?
It seems that pip3 refers to Python-2.7's pip module or any other version of Python-3 that you have installed on your machine. However, you can install packages directly using the intended Python version. You'd need to just use -m option.
python3.6 -m pip install numpy
Another option is to changing the source path that pip3 refers to. You can do this by finding the path of Python-3.6's pip and just bind it to pip3 alias.
Find the absolute path of the python3 interpreter with a command like this:
$ which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python
Your path may be something different, of course. Copy that line to your clipboard.
Edit the pip3 script, which was installed using incorrect interpreter. Something like this:
vi $(which pip3)
You might need to use sudo here, but try it first without. The first line will be something like:
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Change it to the path found in the previous step, e.g.
#!/Library/Frameworks/Python.framework/Versions/3.6/bin/python
Save the pip3 file and exit. This should be sufficient to associate pip3 with the correct environment. Check and verify the result with pip3 --version. Now pip3 install numpy should work as expected.
This works for me:
python3 -m pip install -U --force-reinstall pip

Problems of VirtualEnv and pip3

I used VirtualEnv to create a python2 environment without system site packages like this:
virtualenv -p /usr/bin/python2.7 --no-site-packages ENV2.7
And I want to install packages in this environment.
However, I found that my python code is still trying to look for packages out of this environment.
For example, after activate this env, I used:
pip install matplotlib
And in my demo.py, there is
import matplotlib
But this raised an error, and can not find this package
However, when I use python in the terminal and enter the interactive python, import matplotlib dose not raise an error.
Then I started another terminal and tried to install this package out of the environment by pip3:
pip3 install matplotlib
It turned out that my demo.py just work well.
Any idea? Many Thanks!
It sounds like your virtualenv pip version may be using pip3 instead of pip2:
Make sure you are using the correct python version in your project that you mean to, and using the same version of pip in your virtualenv. (Note that you use pip above once, then you used pip3 outside your virtualenv.)
Check your pip version from inside the virtualenv:
workon (your env name)
which pip
pip -V
Output should look something like:
$ which pip
/home/yourname/.virtualenvs/testenv/bin/pip
$ pip -V
pip 9.0.1 from /home/yourname/.virtualenvs/testenv/local/lib/python2.7/site-packages (python 2.7)
It should tell you you're using pip inside your virtualenv, and the correct python version.
If that looks correct, install your packages.
pip install (whatever)
Check they are installed with pip freeze.
Run your project. :)

bash: virtualenv: command not found "ON Linux"

I am using a form of Lubuntu called GalliumOS (optimized for Chromebooks). I installed pip using $ sudo apt-get install python-pip. I then used pip install --user virtualenv and pip install virtualenv, and then when I tried to subsequently use virtualenv venv I experienced the message bash: virtualenv: command not found.
Between the pip installs above, I used pip uninstall virtualenv to get back to square one. The error remained after a reinstall.
I read several other posts, but all of them seemed to deal with similar problems on MacOS. One that came close was installing python pip and virtualenv simultaneously. Since I had already installed pip, I didn't think that these quite applied to my issue. Why is pip install virtualenv not working this way on LUbuntu / GalliumOS?
Are you sure pip install is "failing"? To me, it sounds like the directory to which pip is installing modules on your machine is not in your PATH environment variable, so when virtualenv is installed, your computer has no idea where to find it when you just type in virtualenv.
Find where pip is installing things on your computer, and then check if the directory where the pyenv executable is placed is in your PATH variable (e.g. by doing echo $PATH to print your PATH variable). If it's not, you need to update your PATH variable by adding the following to your .bashrc or .bash_profile or etc.:
export PATH="PATH_TO_WHERE_PIP_PUTS_EXECUTABLES:$PATH"
What finally worked for me was this. I used
$ sudo apt-get install python-virtualenv.
I was then able to create a virtual environment using $ virtualenv venv.
I was seeking to avoid using $ sudo pip install virtualenv, because of admonitions in other posts to not do this, and agreed, because of experiences I'd had with subsequent difficulties when doing this.
pip install virtualenv
This command worked for me for. This problem that raised to me on Kali Linux.

Categories

Resources