How to modify package before installing it with pip - python

Trying to install a package on Windows fails, as it requires a site.cfg file to contain the path of a library.
It looks like pip extracts packages to c:\users\[username]\appdata\local\temp\pip-install-[random string]\ during installation and deletes after installation (successful or not), so I can't "hot-edit" it.
Can I make pip wait for me before installing?
Can I make pip download and unpack the package and afterwards tell it to install a package from a directory rather than a package name or URL?
Feel free to comment alternative solutions for installing scikits.audiolab on Windows (demands sndfile to be defined in site.cfg).

Does not work on wheels (see #hoefling comment)
Download package from url (pip tells you the url when you do pip install <package_name>)
Untar the file and do your modifications
Then do pip install <path_of_package>

Related

How can i know the name of a imported module? PYTHON

i've installed a python project, and it imports modules(Like almost every project). The problem is when i want to install them(because i haven't got the modules), for example: In the project is imported a module called "a" but when i go and install "a" with pip install a, it says ERROR: Could not find a version that satisfies the requirement a (from versions: none) ERROR: No matching distribution found for a. How could i know the name of the module that is imported in that python project?
Edit:
btw i just found out the module that the project uses comes in the zip where the python project is. How could i install it so it works?
All pip packages are listed here. If you want to import a module called a inside a python script, the command to install it could be sometimes pip install b. Because the name of the stored package can varied from the python import name. To find how to install it the best is to get the pypi url of your package. You can googling the python error ModuleNotFoundError: No module named 'dgvd', it always show you the pypi url in top links.
The good practice in a project is to have a txt file called requirement.txt that you create in bash using this command:
pip freeze > requirement.txt
Then install all packages in once using:
pip install -r requirement.txt
For installing from zip simply use:
pip install *.zip
or specify the path directly:
pip install <path to .zip>
pip install ./my-archive.zip
Same applies for a tarball or any other format. It can be even a folder. However, it has to include a proper setup.py or other mechanism for pip to install it and pip has to support the packaging format (be it archive, networking protocol, version control system (git prefix), etc).
pip install ./my-folder
pip install ./
pip install .
pip install ..
etc
If, however, there is no setup.py present, you'll need to simply copy-paste the files somewhere where your project/module resides (or set PYTHONPATH or sys.path to that folder) to be able to import them. See this or this question for more.

Force pip to skip or ignore bad hash on download cache

Wondering if anyone knows a workaround to force pip to either completely skip hash checks or ignore bad sums when installing from a download cache? Install cmd is:
pip.exe install --target=C:\WHERE_I_WANT_INSTALLED --download-cache=C:\MY_DL_CACHE mitmproxy
Mitmproxy requires a specific version of pillow, and in that specific version there just happens to be a defined C function whos signature collides with another function in an include within Mingw x86_64. I'm not changing out my toolchain, as anyone who uses mingw on windows knows, it's a disgustingly painful process to find and keep a stable version.
Anyway, I've posted the question as a bug report on pips github but I thought I'd pose the question here. Thanks in advance.
Well, I found the answer in the very last place I would ever think to look: the documentation.
So basically you run a few commands to have pip download everything that is required for what you're trying to install. In this case, its was mitmproxy. So first I grabbed the requirements.txt file for mitmproxy, and dropped it into a dir. Commands to download packages were:
pip install --download C:\MY_SECRET_PATH\mitm\dl-cache six
pip install --download C:\MY_SECRET_PATH\mitm\dl-cache mock
pip install --download C:\MY_SECRET_PATH\mitm\dl-cache itsdangerous
pip install --download C:\MY_SECRET_PATH\mitm\dl-cache cryptography
pip install --download C:\MY_SECRET_PATH\mitm\dl-cache mitmproxy
Now, everything required for mitm-proxy is stored in the provided path. We then supply this path and a couple of other flags to the command for installing what we're after, again mitmproxy. To make things more interesting, I'm installing all of this stuff in a custom dir. So that command is as follows:
pip.exe install mitmproxy --no-index --target=C:\MY_SECRET_PATH\mitm --find-links=C:\MY_SECRET_PATH\mitm
So we're basically telling pip to install the selected package and all its deps offline, not checking pypy and therefore skipping hash checks. You're then obviously free to modify the sources of the packages you've downloaded, as I have.

Breaking 'pip install' to smaller steps, so I can edit the package before it is installed

My familiarity with pip ends up with the ability to do: 'pip install', 'pip uninstall', and 'pip list' - with the name of the package I want to install as the single argument.
This limited knowledge carried me so far, to the extent I'm able to install most of the simple packages, and sometime, when I'm luck, I'm even able to install packages that requires compilation. This is all magic for me.
I'm now facing a situation where I need to do a little bit of editing to the C file (side note: this seems to be a known workaround for the 'netifaces' package - which everyone seems to be in peace with. By itself this is an amazing phenomena).
So I would like to break the installation into smaller steps:
Download the egg file (I've figured out this one: pip install --download).
Unzip or otherwise unpackage the package file, to the point I can edit individual
Do my custom modification.
Do the build
Do the installation.
Other than step #1, I don't know how to proceed.
Modern pip (Since 1.10)
Use pip download:
pip download mypackage
pip 1.5 - 1.9
Use pip install -d
pip install -d . --allow-external netifaces --allow-unverified netifaces netifaces
tar xzf netifaces-0.8.tar.gz # Unpack the downloaded file.
cd netifaces-0.8
Now do your modifications and continue:
pip install .
Old pip (Before 1.5)
Install the package with --no-install option; with --no-install option, pip downloads and unpacks all packages, but does not actually install the package.
pip install --no-install netifaces
Change to the build directory. If you don't know where is the build directory, issue above command again, then it display the location.
cd /tmp/pip_build_falsetru/netifaces
Do the custom modification.
Install the package using pip install . (add --no-clean option if you want keep the build directory) or python setup.py install.
sudo pip install --no-clean .
First, download the source to 0.8 from the author's home page (there's no direct download link from PyPI, for some reason). Go to the directory where you downloaded it and unzip it:
tar zxvf netifaces-0.8.tar.gz
Enter the netifaces-0.8/ directory and edit netifaces.c with your favorite editor. Save the file. Then, build the module:
python setup.py build
and install it:
sudo python setup.py install
To test, first leave the directory, then start your python interpreter and import netifaces to see if it works.
Good luck!
Download your selected package, extract the files,edit what you want. Then, open the directory with your terminal\cmd and run:
python setup.py install
Depending on your os you might need to add a little sudo to the beginning of this command (if you intend to install globally on a Unix machine)
You could just download the source from pypi, edit it and use setup.py buid, setup.py install

How to install win32com module in a virtualenv?

I have installed both virtualenv and pywin32 on my Windows 7 64-bit laptop, and each of them seems to work, but not with each other.
More specifically, if a virtualenv is active, then running import win32com.client in a python interactive shell fails with No module named win32com.client. The same expression succeeds, however, if no virtualenv is active.
When I try to install pywin32 with pip (which is how I normally install modules when a virtualenv is active), I get the error:
Could not find any downloads that satisfy the requirement pywin32
No distributions at all found for pywin32
...even though pywin32 is one of the modules listed in the output of
pip search pywin32
Therefore, to install pywin32 at all I had to install it using an *.exe installer downloaded from SourceForge.
Is there any way to install pywin32 within a virtualenv?
UPDATE 2016
There is now a version of pywin32 on PyPI that can be installed with pip. It is called pypiwin32, and it installs the package using the binary wheel format.
https://pypi.python.org/pypi/pypiwin32
pip install pypiwin32
That will work in a virtualenv, or with tox, etc.
Below this line is my previous old answer. That is now outdated information.
OLD ANSWER - OUTDATED. Modern versions of virtualenv default to --no-site-packages. That means that not having access to global site-packages is now the default behavior.
Sadly, (as of July 2014) you can not pip install pywin32 in to your virtualenv. (here's the bug report)
If you want to get pywin32 running inside a virtualenv, activate the virtualenv and use easy_install and the pywin32 installer exe file. For example
easy_install "C:\Path\To\Downloads\pywin32-219.win32-py3.4.exe"
Related question
Try this:
Download directly
Enable your virtualenv
pip install --no-index --find-links:/local/dir/ SomePackage
Check out #8 on pip-installer.org (sorry no permalink)/
Create a virtualenv and activate it:
cd c:\Users\ernesto.luzon
virtualenv --no-site-packages py351env
py351env\Scripts\activate
From here, you have two options:
Download pywin32 from sourceforge project:
http://sourceforge.net/projects/pywin32/files/pywin32/
Download pywin32 from unofficial (but very helpful) binary site:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32
Make sure you download the correct version for the Python Interpreter installed in your environment, otherwise you will encounter ImportError: DLL load failed: %1 is not a valid Win32 application later.
If you downloaded from sourceforge, install it using easy_install:
(py351env) C:\Users\ernesto.luzon\Downloads>easy_install pywin32-220.win-amd64-py3.5.exe
If you downloaded from gohlke, install it using pip:
(py351env) C:\Users\ernesto.luzon\Downloads>pip install pywin32-220-cp35-none-win_amd64.whl
In case you encounter ImportError: DLL load failed: The specified module could not be found error later on, you need these additional steps:
Run the post install script:
(py351env) C:\Users\ernesto.luzon>python py351env\Scripts\pywin32_postinstall.py -install
Copied pythoncom35.dll to C:\Users\ernesto.luzon\py351env\pythoncom35.dll
Copied pywintypes35.dll to C:\Users\ernesto.luzon\py351env\pywintypes35.dll
....
Notice where it copied the 'pythoncom35.dll' and 'pywintypes35.dll' files. You need to move these files to the folder:
C:\Users\ernesto.luzon\py351env\Lib\site-packages\win32

Easy_install cache downloaded files

Is there a way to configure easy_install to avoid having to download the files again when an installation fails?
Update 13 years later: easy_install was removed from Python in January 2021. The python package manager is pip, it caches downloaded packages.
pip (http://pypi.python.org/pypi/pip/) is a drop-in replacement for the easy_install tool and can do that.
Just run easy_install pip and set an environment variable PIP_DOWNLOAD_CACHE to the path you want pip to store the files.
Note that the cache won't work with dependencies that checkout from a source code repository (like svn/git/hg/bzr).
Then use pip install instead of easy_install
Here is my solution using pip, managing even installation of binary packages and usable on both, Linux and Windows. And as requested, it will limit download from PyPi to the minimum, and as extra bonus, on Linux, it allows to speed up repeated installation of packages usually requiring compilation to a fraction of a second.
Setup takes few steps, but I thing, it is worth to do.
Create pip config file
Create pip configuration file (on linux: ~/.pip/pip.conf, on Windows %HOME%\pip\pip.ini)
My one has this content:
[global]
download-cache = /home/javl/.pip/cache
find-links = /home/javl/.pip/packages
[install]
use-wheel = yes
[wheel]
wheel-dir = /home/javl/.pip/packages
Populating cache dir - goes automatically
The cache dir will get cached version of data downloaded from pypi each time, pip attempts to get some package from pypi. It is easy to get it there (no special care needed), but note, that from pip point of view, these are just cashed data downloaded from PyPi, not packages, so in case you use an option --no-index, it will not work.
pip download to populate packages dir
The packages dir is place to put real package files to. E.g. for my favorite package plac, I would do:
$ pip download --dest ~/.pip/packages plac
and the plac package file would appear in that dir. You may even use -r requirements.txt file to do this for multiple packages at once.
These packages are used even with $ pip install --no-index <something>.
Prevent repeated compilation of the same package on Linux
E.g. lxml package requires compliation, and download and compile may take from 45 seconds to minutes. Using wheel format, you may save here a lot.
Install wheel tool, if you do not have it yet:
$ pip install wheel
Create the wheel for lxml (assuming, you have managed to install lxml in past - it requires some libs in the system to be installed):
$ pip wheel lxml
This goes over download, compile, but finally results in lxml whl file being in packages dir.
Since then
$ pip install lxml
or even faster
$ pip install --no-index lxml
will take fraction of a second, as it uses wheel formatted package.
Prepare wheel package from Window setup exe package
(note: this can be prepared even on Linux machine, there is no compilation, only some repacking from exe file into whl.)
download the exe form of the package from pypi, e.g:
$ wget https://pypi.python.org/packages/2.7/l/lxml/lxml-3.2.3.win32-py2.7.exe#md5=14ab978b7f0a3382719b65a1ca938d33
$ dir
lxml-3.2.3.win32-py2.7.exe
convert it to whl
$ wheel convert lxml-3.2.3.win32-py2.7.exe
$ dir
lxml-3.2.3.win32-py2.7.exe
lxml-3.2.3-cp27-none-win32.whl
Test it:
$ pip install lxml
or
$ pip install --no-index lxml
shall be very quick.
Note, that wheel convert can do exactly the same conversion for egg formatted packages.
Let easy_install and setup.py install reuse your packages dir
easy_install and $ python setup.py install do not seem to offer download cache, but allow to use packages we have in our packages dir.
To do so, edit config file for these two tools:
On Linux: $HOME/.pydistutils.cfg
On Windows: %HOME%\pydistutils.cfg
In my case I have here in /home/javl/.pydistutils.cfg:
[easy_install]
find_links = /home/javl/.pip/packages
This config may help even some cases of pip install calls, when pip attempts to install a package, declaring dependency on other ones. As it delegates this task to setup.py call, without the .pydistutils.cfg config it would download the file from PyPi.
Unfortunately, wheel format is not supported in this case (as far as I am aware of).

Categories