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.
Related
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.
I'm trying to work with pipenv and I installed it using pip, however whenever I run any command starting with pipenv, I get the following error:
zsh: command not found: pipenv
I know I should add it to my path somehow but I'm not entirely familiar with how to configure my ~/.zshrc.
Also, I tried locating where pipenv is located using where pipenv, but I get
pipenv not found
You will need to add support to your ~/.zshrc file. You can access it with code ~/.zshrc.
I needed to add these to the file:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PYTHON_BIN_PATH="$(python3 -m site --user-base)/bin"
export PATH="$PATH:$PYTHON_BIN_PATH"
Once you add them you will need to restart your terminal before you can see these changes take effect.
Using pip
pipenv should be in your PATH if you installed it using pip as a user install, as recommended in the docs:
pip install --user pipenv
If it still isn't available, you'll need to add the user base's binary directory to your PATH:
UNIX default: ~/.local
MacOS Framework builds: ~/Library/Python/X.Y
Windows: %APPDATA%\Python
You can read more about this in the Python docs on site.USER_BASE.
Using Homebrew
If you're using Homebrew, then another option is to install pipenv like so:
brew install pipenv
This installs it globally. Since pipenv can manage even different python versions via pyenv, it's preferable to have it set up like this instead of installing it only for a specific python version using pip.
However, this method is discouraged according to the pipenv documentation:
Homebrew installation is discouraged because each time the Homebrew Python is upgraded, which Pipenv depends on, users have to re-install Pipenv, and perhaps all virtual environments managed by it.
Use a package manager such as apt, yum or brew to install pipenv.
Installing pipenv with a package manager rather than pip adds it directly to $PATH of any shell, like bash or zsh.
sudo apt install pipenv
Open ~/.zshrc and append these lines:
# Setting PATH for Python 3.4
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
or:
# Setting PATH for Python 3.9
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
I tested it on macOS.
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?
As we all know, Mac OS ships with its own python pre installed.
The recommendation seems to be to leave that alone and use homebrew to install a fresh python into the system.
My issue is that after installing python (and pip) using homebrew, pip is installing packages into the Mac OS site-packages instead of my own. I have confirmed I am running the "homebrew" pip:
$ which pip
/usr/local/bin/pip
But then when I pip install something I can se it is installed at:
/lib/python2.7/site-packages
Pip should be installing at /usr/local/lib/python2.7/site-packages unless i'm miss understanding something.
The surprising thing is that checking with -V yields a surprising result:
pip -V
pip 7.1.0 from /usr/local/lib/python2.7/site-packages (python 2.7)
Running pip list just after running pip install does not show the packages that were supposedly just installed by it but went to the wrong site-packages.
Adding to this, the packages installed on the /lib/python2.7/site-packages are not recognized by my $PYTHONPATH and as such I cannot use them.
To add even more confusion, I decided to use a virtualenv, but I was amazed as even using pip with the virtualenv active kept installing to the /lib/python2.7/site-packages instead of the virtualenv site-packages.
So, somehow I ended up with a homebrew pip, that installs packages outside of the homebrew site-packages and a python interpreter that cant use the packages installed by pip.
How do you recommend I go about finding the root cause and having a smooth python experience? :)
You can easily find you site-packages directory by invoking this command
python -c 'import site; print(site.getsitepackages())'
I think after you activate a virtualenv your python path should point to that environments site-package location--if not it's probably not activated. Only once you activate it will you run pip so it installs in that virtual env's site-packages. if it's not activated, it will go in whatever other site-packages it already knows about:
Step 1: create a virtual env
a la... virtualenv venv
Do this only once!
Step 2: Activate the vitual env
something like source /venv/bin/activate
Needs doing every time you want to use this virtual environment
Step 3: run pip commands, watch them get installed in the virtual env site-packages!
If you do step 3 before step 2 your not actually using the virtual environment you created, so all bets are off--That's probably the reason pip is still installing to the old location.
Now, my overall recommendation is to go further and use pyenv to install specific version of python into your /Users/username/.pyenv folder and abandon both the default OSX and homebrew packages. It's simple and you can control easily the exact version of python to use by simple issuing of command to change versions.
THEN use virtualenv in python2 or pyvenv if in python3 (not to be confused with pyenv) to build vitual environments with their own local site-packages to store pip modules. When you activate a virtualenv, your $PYTHONPATH will switch to the specific location.
The flow would then be:
Use pyenv to pull down and switch to a specific version of python you want to use--overriding homebrew and the OSX version.
Create your vitrualenv. This will create a bin that will link to the pyenv python stack you just specified in the previous step.
Activate the virtual env, and proceed.
Totally control your environment!
For one, you could try updating pip with pip install --upgrade pip command, which might or might not redirect your pip path.
Two, and I should have really started with this one is to set the preferred pip executable path in the .bash_profile or .zshrc if you are using one. The way you do it (on Mac) is by holding Shift+Command+Period to reveal hidden files, going to the User folder and opening the .bash_profile/.zshrc with a text editor. Afterward, add the path/to/bin where the pip that you require is. Like export PATH="User/Username/anaconda3/bin:$PATH" or /usr/local/bin or path/to/venv/bin. Whatever code you write in the end will overwrite the previous one.
Three, if you don't wanna change your default pip, but rather wanna use a different version for that specific case just include the full path of the pip executable like /usr/local/bin/pip list or Users/Username/Desktop/venv/bin/pip install module.
I do not have root previlege on a linux server so I want to creat a virtual python according to creating a "virtual" python.
After I run virtual-python.py, I do have python in ~/bin/python:
Then, according to setuptools PyPI page, I download ez_setup.py and run ~/bin/python ez_setup.py. Error occurs:
What should I do?
Looking at the linked website, it looks outdated. You use pip, not easy_install.
For installing development packages, I always take the following rules in account:
The system package manager is responsible for system-wide packages, so never use sudo pip. This doesn't just match the question, but this is always a good idea.
The package manager packages are probably outdated. You'll want an up-to-date version for development tools.
I recommend the following way to install local development tools.
$ # Install pip and setuptools on a user level
$ curl https://bootstrap.pypa.io/get-pip.py | python - --user
$ # Add the executables to your path. Add this to your `.bashrc` or `.profile` as well
$ export PATH=$PATH/$HOME/.local/bin
At this point pip should be accessible from the command line and usable without sudo. Use this to install virtualenv, the most widely used tools to set up virtual environments.
$ pip install virtualenv --user
Now simply use virtualenv to set up an environment to run your application in:
$ virtualenv myapp
Now activate the virtual environment and do whatever you would like to do with it. Note that after activating the virtual environment, pip refers to pip installed inside of the virtualenv, not the one installed on a user level.
$ source myapp/bin/activate
(myapp)$ pip install -r requirements.txt # This is just an example
You'll want to create a new virtual environment for each application you run on the server, so the dependencies can't conflict.