python install on machine with no internet but has basic python interpreter - python

following is my question/problem:
I am have installed python on two machine, machine A with internet ,machine B without internet.
I need to install packages(say for example pillow package) on machine B
I tried :pip download pillow ,on machine a in folder.
It created wheel file which does not work on machine B (some packages download as zip which can be installed on machine B ,but not one with wheel file.
I am trying route of virtual env.
On machine A I am doing :1. C:\pro1> myenv\scripts\activate
myenv C:\vi\pro1> pip install pillow
Taking the whole folder to machine B .
Assuming it should work as the package is in virtual env folder ,but it doesn't . :(
How can I make pillow package work on offline machine ?
Thank you.

Try to follow this. Obviously you must replace the packages with the ones you need.
virtualenv my-new-virtual-env
cd my-new-virtual-env
Activate the environment using the commands shown above.
For our convenience lets create in the root folder of the env a Wheelhouse/Tarhouse folder which will we install all our packages.
(Windows) mkdir Wheelhouse and afterwards cd Wheelhouse
(Linux) mkdir Tarhouse and afterwards cd Tarhouse
pip download virtualenv django numpy
pip freeze > requirements.txt
Take the downloaded .whl/.tar files to an offline station
Make sure you are in the root folder of the virtual envrionment and
issue the following command in your command line:
pip install -r requirements.txt --find-links=(Wheelhouse or Tarhouse)
I just wanted to add up that you can issue the command(if you want
install single package at a time):
pip install Wheelhouse/some-package-file.whl (or .tar on linux)

Related

Is there any way to package python code so that other machine does not need to install all the dependencies using pip

I have installed python in my system and wrote a simple script using GET REST API for Jenkins data.
I have installed all the required modules using pip. Now I want to package this script with all the dependencies and run on another machine. However, in another machine, I don't want to perform all the pip installation steps.
I know we can mention all the modules in the requirements.txt and use pip install -r requirements.txt. But, is there any way so that I don't need to install modules using pip for each dependency, such that I can install Python and all other dependencies must be installed when I run the zip file.
You can install pip dependencies to a certain directory using -t (target).
pip install -r requirements.txt -t .
That will install your pip modules to the current directory. You can zip the whole thing then and deploy. Make sure that the environment you install the dependencies in matches your intended deployment environment. For consistency you can run the command in a docker container, for example.
I think you should use virtualenv module which makes your project easily deploy-able.
Virtual Environment should be used whenever you work on any Python based project. It is generally good to have one new virtual environment for every Python based project you work on. So the dependencies of every project are isolated from the system and each other.
I came across a link which can help Virtual Env explained

Installing Python Dependencies locally in project

I am coming from NodeJS and learning Python and was wondering how to properly install the packages in requirements.txt file locally in the project.
For node, this is done by managing and installing the packages in package.json via npm install. However, the convention for Python project seems to be to add packages to a directory called lib. When I do pip install -r requirements.txt I think this does a global install on my computer, similar to nodes npm install -g global install. How can I install the dependencies of my requirements.txt file in a folder called lib?
use this command
pip install -r requirements.txt -t <path-to-the-lib-directory>
If you're looking to install dependencies in special (non-standard) local folder for a specific purpose (e.g. AWS Lambda), see this question: install python package at current directory.
For normal workflows the following is the way to install dependencies locally (instead of globally, equivalent to npm i instead of npm i -g in Node):
The recommended way to do this is by using a virtual environment. You can install virtualenv via pip with
pip install virtualenv
Then create a virtual environment in your project directory:
python3 -m venv env # previously: `virtualenv env`
Which will create a directory called env (you can call it anything you like though) which will mirror your global python installation. Inside env/ there will be a directory called lib which will contain Python and will store your dependencies.
Then activate the environment with:
source env/bin/activate
Then install your dependencies with pip and they will be installed in the virtual environment env/:
pip install -r requirements.txt
Then any time you return to the project, run source env/bin/activate again so that the dependencies can be found.
When you deploy your program, if the deployed environment is a physical server, or a virtual machine, you can follow the same process on the production machine. If the deployment environment is one of a few serverless environments (e.g. GCP App Engine), supplying a requirements.txt file will be sufficient. For some other serverless environments (e.g. AWS Lambda) the dependencies will need to be included in the root directory of the project. In that case, you should use pip install -r requirements.txt -t ./.
I would suggest getting the Anaconda navigator.
You can download it here: https://www.anaconda.com
Anaconda allows you to create virtual environments through a graphical interface. You can download any pip package that is available through Anaconda.
Then all you have to do after you have created and added onto your environment is to got to your designated python editor (I mainly use Pycharm) and setting the path to the virtual environment’s interpreter when you select or change the interpreter for your project.
Hope this helps.

Option to import global packages into virtual environment I create?

I am new to using python, so bear with me if I make any assumptions.. So I have virtualenv and pip installed in my ubuntu machine. Every time I create a virtual environment I have to remotely download and install python modules(using pip install) such as django already installed in the main python package.
The problem is that I am not always connected to the internet. Is there a way I can load modules existing in the main Python to every virtual environment I create? Thanks!
You can pip download the Python packages you want to install offline and then install the .whl files in your virtualenv. Here is an example with Django and Requests:
Create a directory to store local Python packages:
mkdir local_python
Change directory: cd local_python
Download Python packages to be available offline:
pip download django requests
Install local package .whl files after you activate your virtualenv:
pip install Django-1.11.1-py2.py3-none-any.whl requests-2.16.5-py2.py3-none-any.whl

Installing Django and related packages on an offline computer

I'm using win7, python 2.7 and I have a project with several packages running.
I wanted to move the project to my friends laptop (which can't access to internet, not an option). so I downloaded Python/Django/All required packages, Installed python and run python setup.py install in each package directory.
I've found that some packages, even after I download them, requires somethings from the internet to be downloaded and causes error. so:
How can I download a package and all it's dependencies or what ever it needs to be installed offline?
This is how you can do it
Install Django and all related packages to a Python virtual environment
Run pip freeze > requirements.txt which will list all installed packages and their versions in this file
Use pip wheel -r requirements.txt command which builds a wheelhouse folder of the package list
Zip this folder
Go to your friend's computer, unzip
create virtual environment and run pip install wheelhouse/* (Install all packages from the wheelhouse)
More about pip wheel.
Python and pip needs to be separately copied and installed.

Python: PIL/_imaging.so: invalid ELF header

I'm using a virtualenv to run Python 2.7 in my local machine and everything works as expected. When I transfer "site-packages" to my production sever, I get the follow error:
PIL/_imaging.so: invalid ELF header
This happens on the Pillow 2.5.3 pypi package found here
I am running OS X, while my production server is running Debian. I suspect the OS differences might be causing the issue but I'm not sure. I have no idea how to fix this. Can anyone help?
Note: I cannot install packages directly to my production server, so I have to upload them directly to use them.
In your current virtual environment, execute the following command
pip freeze > requirements.txt
Copy this requirements.txt file to your server.
Create your new virtualenvironment (delete the one you were using before).
Activate the virtual environment and then type pip install -r requirements.txt
Now, the libraries will be installed correctly and built accurately as well.
If you see errors for PIL, execute the following commands:
sudo apt-get install build-essential python-dev
sudo apt-get build-dep python-imaging
virtual environments are for isolating Python on your current machine; they are not for creating portable environments. The benefit is to work with different versions of Python packages without modifying the system Python installation.
Using virtual environments does not require super-user permissions; so you can install packages even if you are not "root".
It does, however, require Internet access as packages are downloaded from the web. If your server does not have access to the Internet, back on your mac, do the following, from your virtual environment:
pip install basket
This will install basket which is a small utility that allows you to download packages but not install them. Great for keeping a local archive of packages that you can move to other machines.
Once its installed, follow these steps as listed in the documentation:
basket init
pip freeze > requirements.txt
awk -F'==' '{print $1}' requirements.txt | basket download
This will download all the packages from your requirements.txt file into ~/.basket
Next, copy this directory to your server and then run the following command from your virtualenvironment
pip install --no-index -f /path/to/basket -r requirements.txt

Categories

Resources