Apporto & Python Modules

Summary

When using Python on Apporto, you will often need to work with Python modules. However, due to security restrictions on Apporto VMs, you cannot install libraries directly onto the C drive. Instead, you can install libraries into your user-specific directory.

How to install Modules

  1. To install modules, use pip with the --target option to specify a directory where the module should be installed.
  2. Use the following command to install the module in your directory:

                        pip install --target Z:\path\to\your\directory your_module

                Example

                        pip install --target Z:\python-modules psycopg2

Code for Python Script

Once you have installed a module, you need to tell Python where to find it. To do this, add the directory containing the installed module to Python's sys.path at the beginning of your script.

Code:

 

import sys

# Specify the directory where your package is installed

package_directory = r'Z:\path\to\your\package\directory'

sys.path.append(package_directory)

# Now you can import modules from the specified directory

import my_module

# Rest of your script

 

Example:

 

import sys

# Add the psycopg2 package directory

package_directory = r'Z:\python_modules'

sys.path.append(package_directory)

# Import the module

import psycopg2

# Rest of your script

 

Key Points:

  • Always replace Z:\path\to\your\directory with the actual path to your target directory.
  • Use the r prefix for raw strings to avoid issues with backslashes in file paths.
  • Ensure the directory has sufficient space for the modules being installed.
Was this helpful?
0 reviews