Can I install Python windows packages into virtualenvs?
I ended up adapting a script (http://effbot.org/zone/python-register.htm) to register a Python installation in the registry. I can pick the Python to be the Python in the registry, run the Windows installer, then set the registry back:
# -*- encoding: utf-8 -*-
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# Adapted by Ned Batchelder from a script
# written by Joakim Löw for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
import sys
from _winreg import *
# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix
regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
installpath, installpath, installpath
)
def RegisterPy():
try:
reg = OpenKey(HKEY_LOCAL_MACHINE, regpath)
except EnvironmentError:
try:
reg = CreateKey(HKEY_LOCAL_MACHINE, regpath)
except Exception, e:
print "*** Unable to register: %s" % e
return
SetValue(reg, installkey, REG_SZ, installpath)
SetValue(reg, pythonkey, REG_SZ, pythonpath)
CloseKey(reg)
print "--- Python %s at %s is now registered!" % (version, installpath)
if __name__ == "__main__":
RegisterPy()
Run this script with the Python you want to be registered, and it will be entered into the registry. Note that on Windows 7 and Vista, you'll need Administrator privileges.
how to install python packages locally inside virtual environment
First, create a virtual environment for installing it for project-specific.
For that, you will require virtualenv package. You can download it using:
pip install virtualenv
Once done, move to your project directory and create virtualenv using:
virtualenv <your_environment_name>
Like if you wants to create environment named my_env. The command goes like:
virtualenv my_env
Now activate your virtualenv.
If you are on windows, activate using:
.\my_env\Scripts\activate
If you are on Linux:
source my_env/bin/activate
Now install your requirements.txt using:
pip install -r requirements.txt
How to install a Python package inside a virtual environment with Pip (OS X)
An introductory overview is available in this nice tutorial. Here is a good summary with more detail. But, if you renamed or moved the virtual env dir after its creation, it could break it. Create a new one from scratch: $ cd ~/PycharmProjects; python3 -mvenv newenv
; Activate: $ source newenv/bin/activate
; Install something: $ pip install colorama
(same as pip3 install
only if venv activated); Check: ls ~/PycharmProjects/newenv/lib/python3*/site-packages
; Deactivate: $ deactivate
Then you could try this solution for Pycharm: how to associate a virtual environment with a python project in pycharm. PyCharm indeed comes bundled with virtualenv
which could have been customized, please look into Pycharm-specific resources: creating virtual environments and installing packages in Pycharm.
If you have installed PyPI's mainstream virtualenv, by default it will create new environments with the python interpreter that virtualenv
was installed with. But it's possible to specify an alternate Python Interpreter upon a new env creation: $ virtualenv -p python3.7 newenvname
Regarding the error DistutilsOptionError: must supply either home or prefix
- please check this and this for solutions. Homebrew'ed mappings between python and pip are described here. The normal pip install --user
is disabled in homebrewed Python, but there are workarounds. MacOS system Python doesn't provide pip
, but it can installed, reinstalled or upgraded for any specific python version manually. Original non-brewed installers are also available for all Python versions: https://www.python.org/downloads/mac-osx/
By default there's no pip.conf
, but it can be created by hand to customize things. All possible pip.conf locations (per-user, per-venv, and global/system-wide, and how they override each other) are listed here. If someone faces an issue, they could use pip config list
command to see their active configuration, or locate pip.conf
and find
it.
Finally, you may want to ensure you aren't using pip
against macOS' system python. Shell commands such as $ brew info python
, which pip
, which pip3
, pip3 -V
, which python3
can help you see what you actually use. Since the macOS default $PATH used to be /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
, stock macOS binaries (including python) may take precedence over some homebrew'ed installations (including python). If so, a custom PATH could be exported via the ~/.bashrc
if required.
How to get pip to install packages into the virtual environment?
It works well for me after following docs:
Common installation tools such as
Distribute
andpip
work as expected
with venvs - i.e. when a venv is active, they install Python packages
into the venv without needing to be told to do so explicitly. Of
course, you need to install them into the venv first: this could be
done by runningdistribute_setup.py
with the venv activated, followed
by runningeasy_install pip
. Alternatively, you could download the
source tarballs and runpython setup.py install
after unpacking, with
the venv activated.
How to force install package in virtualenv?
The problem was in Webfaction VPS
Need an empty file named sitecustomize.py in the /home/username/webapps/appName/env/lib/python2.
That empty file overrides their python customizations, one of which is to include any packages in the ~/lib/python2.7 directory.
You might need to deactivate your virtual env and activate it again for changes to take effect.
pip installing in global site-packages instead of virtualenv
Funny you brought this up, I just had the exact same problem. I solved it eventually, but I'm still unsure as to what caused it.
Try checking your bin/pip
and bin/activate
scripts. In bin/pip
, look at the shebang. Is it correct? If not, correct it. Then on line ~42
in your bin/activate
, check to see if your virtualenv path is right. It'll look something like this
VIRTUAL_ENV="/Users/me/path/to/virtual/environment"
If it's wrong, correct it, deactivate
, then . bin/activate
, and if our mutual problem had the same cause, it should work. If it still doesn't, you're on the right track, anyway. I went through the same problem solving routine as you did, which pip
ing over and over, following the stack trace, etc.
Make absolutely sure that
/Users/kristof/VirtualEnvs/testpy3/bin/pip3
is what you want, and not referring to another similarly-named test project (I had that problem, and have no idea how it started. My suspicion is running multiple virtualenvs at the same time).
If none of this works, a temporary solution may be to, as Joe Holloway said,
Just run the virtualenv's pip with its full path (i.e. don't rely on searching the executable path) and you don't even need to activate the environment. It will do the right thing.
Perhaps not ideal, but it ought to work in a pinch.
Link to my original question:
VirtualEnv/Pip trying to install packages globally
Related Topics
Plotting 3-Tuple Data Points in a Surface/Contour Plot Using Matplotlib
Getting Segmentation Fault Core Dumped Error While Importing Robjects from Rpy2
What Is a "Good" Palette for Divergent Colors in R? (Or: Can Viridis and Magma Be Combined Together)
How Is the Feature Score(/Importance) in the Xgboost Package Calculated
Calling Custom Functions from Python Using Rpy2
Typeerror: Use() Got an Unexpected Keyword Argument 'Warn' When Importing Matplotlib
How to Set the R_Home Environment Variable to the R Home Directory
R Foverlaps Equivalent in Python
Error When Installing Rpy2 Module in Python with Easy_Install
Matplotlib Analog of R's 'Pairs'
Equivalent of a Python Dict in R
Closest Equivalent of a Factor Variable in Python Pandas
Comparison of R, Statmodels, Sklearn for a Classification Task with Logistic Regression
Data Scraping from Published Power Bi Visual
What Are the Python Equivalents to Ruby's Bundler/Perl's Carton