How to install packages offline?
On the system that has access to internet
The pip download
command lets you download packages without installing them:
pip download -r requirements.txt
(In previous versions of pip, this was spelled pip install --download -r requirements.txt
.)
On the system that has no access to internet
Then you can use
pip install --no-index --find-links /path/to/download/dir/ -r requirements.txt
to install those downloaded modules, without accessing the network.
If you want install python libs and their dependencies offline, finish following these steps on a machine with the same os, network connected, and python installed:
1) Create a requirements.txt
file with similar content (Note - these are the libraries you wish to download):
Flask==0.12
requests>=2.7.0
scikit-learn==0.19.1
numpy==1.14.3
pandas==0.22.0
One option for creating the requirements file is to use pip freeze > requirements.txt
. This will list all libraries in your environment. Then you can go in to requirements.txt
and remove un-needed ones.
2) Execute command mkdir wheelhouse && pip download -r requirements.txt -d wheelhouse
to download libs and their dependencies to directory wheelhouse
3) Copy requirements.txt into wheelhouse
directory
4) Archive wheelhouse into wheelhouse.tar.gz
with tar -zcf wheelhouse.tar.gz wheelhouse
Then upload wheelhouse.tar.gz
to your target machine:
1) Execute tar -zxf wheelhouse.tar.gz
to extract the files
2) Execute pip install -r wheelhouse/requirements.txt --no-index --find-links wheelhouse
to install the libs and their dependencies
If the package is on PYPI, download it and its dependencies to some local directory. E.g.
$ mkdir /pypi && cd /pypi $ ls -la -rw-r--r-- 1 pavel staff 237954 Apr 19 11:31 Flask-WTF-0.6.tar.gz -rw-r--r-- 1 pavel staff 389741 Feb 22 17:10 Jinja2-2.6.tar.gz -rw-r--r-- 1 pavel staff 70305 Apr 11 00:28 MySQL-python-1.2.3.tar.gz -rw-r--r-- 1 pavel staff 2597214 Apr 10 18:26 SQLAlchemy-0.7.6.tar.gz -rw-r--r-- 1 pavel staff 1108056 Feb 22 17:10 Werkzeug-0.8.2.tar.gz -rw-r--r-- 1 pavel staff 488207 Apr 10 18:26 boto-2.3.0.tar.gz -rw-r--r-- 1 pavel staff 490192 Apr 16 12:00 flask-0.9-dev-2a6c80a.tar.gz
Some packages may have to be archived into similar looking tarballs by hand. I do it a lot when I want a more recent (less stable) version of something. Some packages aren't on PYPI, so same applies to them.
Suppose you have a properly formed Python application in ~/src/myapp
. ~/src/myapp/setup.py
will have install_requires
list that mentions one or more things that you have in your /pypi
directory. Like so:
install_requires=[
'boto',
'Flask',
'Werkzeug',
# and so on
If you want to be able to run your app with all the necessary dependencies while still hacking on it, you'll do something like this:
$ cd ~/src/myapp $ python setup.py develop --always-unzip --allow-hosts=None --find-links=/pypi
This way your app will be executed straight from your source directory. You can hack on things, and then rerun the app without rebuilding anything.
If you want to install your app and its dependencies into the current python environment, you'll do something like this:
$ cd ~/src/myapp $ easy_install --always-unzip --allow-hosts=None --find-links=/pypi .
In both cases, the build will fail if one or more dependencies aren't present in /pypi
directory. It won't attempt to promiscuously install missing things from Internet.
I highly recommend to invoke setup.py develop ...
and easy_install ...
within an active virtual environment to avoid contaminating your global Python environment. It is (virtualenv that is) pretty much the way to go. Never install anything into global Python environment.
If the machine that you've built your app has same architecture as the machine on which you want to deploy it, you can simply tarball the entire virtual environment directory into which you easy_install
-ed everything. Just before tarballing though, you must make the virtual environment directory relocatable (see --relocatable option). NOTE: the destination machine needs to have the same version of Python installed, and also any C-based dependencies your app may have must be preinstalled there too (e.g. say if you depend on PIL, then libpng, libjpeg, etc must be preinstalled).
offline python. for doing this I use virtualenv (isolated Python environment)
1) install virtualenv online with pip:
pip install virtualenv --user
or offline with whl: go to this link , download last version (.whl or tar.gz) and install that with this command:
pip install virtualenv-15.1.0-py2.py3-none-any.whl --user
by using --user
you don't need to use sudo pip…
.
2) use virtualenv
on online machine select a directory with terminal cd
and run this code:
python -m virtualenv myenv
cd myenv
source bin/activate
pip install Flask
after installing all the packages, you have to generate a requirements.txt
so while your virtualenv is active, write
pip freeze > requirements.txt
open a new terminal and create another env like myenv2
.
python -m virtualenv myenv2
cd myenv2
source bin/activate
cd -
ls
now you can go to your offline folder where your requirements.txt
and tranferred_packages
folder are in there. download the packages with following code and put all of them to tranferred_packages
folder.
pip download -r requirements.txt
take your offline folder to offline computer and then
python -m virtualenv myenv2
cd myenv2
source bin/activate
cd -
cd offline
pip install --no-index --find-links="./tranferred_packages" -r requirements.txt
what is in the folder offline [requirements.txt , tranferred_packages {Flask-0.10.1.tar.gz, ...}]
check list of your package
pip list
note: as we are in 2017 it is better to use python 3. you can create python 3 virtualenv with this command.
virtualenv -p python3 envname