This post will summarize how to install a Python package in your local app directory, instead of globally in the user path.
As a developer, you create a Python program that uses a non-standard packages such as pandas, pyodbc, twilio, etc. that isn't isn't included in the python install. You are able to install the packages on your PC using "pip install ..." because your PC has access to the internet.
Now you want to move your Python program to your production app server, however the app server doesn't have access to the internet. The Python code won't run because the non-standard packages aren't on the server, and you can't "pip install" them from the internet. What to do!
The following solution works well for me in the case. I'll discuss this solution using pandas as the example of a non-standard package, and I'll show the code below.
Create your initial Python app like you normally would (however you prefer). In my case I create an empty folder, open the folder using VS Code, add a file named app.py and create a launch.json file.
Add a subfolder to your project folder named "pkg".
Go to pypi.org (or your preferred package website) and search for your package ("pandas" in this example).
On the left navigation select "Download Files". Look for a current version of the package appropriate for the type of server/PC that you will be running the package on. If the server/PC is Windows, then look for a file name that ends in "...amd64.whl".
In my case I download: pandas-2.3.1-cp313-cp313-win_amd64.whl
Save the .whl file in your pkg directory.
What is a Wheel (whl) file? - A .whl file is a build distribution format for Python packages, somewhat like a .zip file. It contains pre-compiled binaries for the library/package that you want to use.
Open Powershell. Change directory to your pkg folder (ex. cd C:\myproject\pkg)
Install the package: pip install pandas-2.3.1-cp313-cp313-win_amd64.whl --target ./
Note: the --target tells pip to install the package in a specific directory, and the ./ indicates the current directory.
For example:
import sys sys.path.append(".\\pkg") import pandas as pd# Create a dictionary with sample data data = {"Name": ["Alice", "Bob", "Charlie"],"Age": [25, 30, 35],"City": ["New York", "Los Angeles", "Chicago"] }# Convert the dictionary into a DataFrame df = pd.DataFrame(data)# Display the DataFrame print("Original DataFrame:") print(df)
Note the first two lines. These are important. The second line adds your local package folder to the system path, which is one of the locations python looks for imported modules.
Run your program. It will work, even though you don't have pandas installed globally on your PC/Server (which you can confirm by using "pip list").
Copy the folder containing your python code to your application server. You can then do "python app.py" on the server and the code will run even though you never did "pip install panadas" on the server.
Do I have to use a wheel file?
No, on your PC (that has internet access) instead of the commands in step 3 above, you can do:
Change directory to your pkg folder (ex. cd C:\myproject\pkg)
Install the package: pip install pandas --target ./
This will install pandas from the internet to your local pkg folder just like it would from the local wheel file. I intially though that everying pandas needed was contained in the wheel file, and this may be true from some packages, both others like pandas will still need to pull dependencies from the internet even when using the wheel file.
If you're copying your Python code that uses non-standard packages to an application server that doesn't have internet access - so you can't "pip install" - you can you install the packages to a local directory in the app folder on your development PC, modify your code to code to check the local directory for the packages, and then the Python code will run as expected on the server.