tar.gz
is the source code of the library. You should unpack it, and you will find a setup.py
script inside. Run:
python setup.py install--prefix = /share/pythonLib
This will create:
/share/pythonLib / lib / python2 .7 / site - packages /
The tarfile module makes it possible to read and write tar archives, including those using gzip, bz2 and lzma compression. Use the zipfile module to read or write .zip files, or the higher-level functions in shutil.,reads and writes gzip, bz2 and lzma compressed archives if the respective modules are available.,The tarfile module provides a simple command-line interface to interact with tar archives.,How to create an archive and reset the user information using the filter parameter in TarFile.add():
$ python - m tarfile - c monty.tar spam.txt eggs.txt
$ python - m tarfile - c monty.tar life - of - brian_1979 /
$ python - m tarfile - e monty.tar
$ python - m tarfile - e monty.tar other - dir /
$ python - m tarfile - l monty.tar
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
The package can now be imported in Python scripts. You may need to run as sudo if you have root privileges, or append --user to install under your home directory (often this will be under $HOME/.local).,A source distribution provides everything needed to build/install the package on any supported platform. Testsuites, documentation and supporting data can also be included.,You can now test pip install from the command line. E.g. To install package pyexample into your user install space:,If you now pip install again and run the Python interpreter you should be able to access these variables:
pip install mpi4py
pip install mpi4py == 2.0 .0
pip install mpi4py ==
pip search mpi4py
$ python
Python 3.6 .3 | Intel Corporation | (
default, Oct 16 2017, 15: 28: 36).... >>>
import mpi4py >>>
mpi4py.__path__['/home/shudson/miniconda3/lib/python3.6/site-packages/mpi4py'] >>>
mpi4py.__version__ '2.0.0' >>>
mpi4py.__file__ '/home/shudson/miniconda3/lib/python3.6/site-packages/mpi4py/__init__.py'
pip install mpi4py
pip install mpi4py == 2.0 .0
pip install mpi4py ==
$ python
Python 3.6 .3 | Intel Corporation | (
default, Oct 16 2017, 15: 28: 36).... >>>
import mpi4py >>>
mpi4py.__path__['/home/shudson/miniconda3/lib/python3.6/site-packages/mpi4py'] >>>
mpi4py.__version__ '2.0.0' >>>
mpi4py.__file__ '/home/shudson/miniconda3/lib/python3.6/site-packages/mpi4py/__init__.py'
E.g. A simple project may have this structure:
pyexample├── LICENSE├── pyexample│├── __init__.py│├── module_mpi4py_1.py│├── module_numpy_1.py│└── module_numpy_2.py├── README.rst└── setup.py
11/02/2021
You can import a Python 3 package and its dependencies by importing the following Python script into a Python 3 runbook, and then running it.
https: //github.com/azureautomation/runbooks/blob/master/Utility/Python/import_py3package_from_pypi.py
With the package imported, you can use it in a runbook. Add the following code to list all the resource groups in an Azure subscription.
import os
import azure.mgmt.resource
import automationassets
def get_automation_runas_credential(runas_connection):
from OpenSSL
import crypto
import binascii
from msrestazure
import azure_active_directory
import adal
# Get the Azure Automation RunAs service principal certificate
cert = automationassets.get_automation_certificate("AzureRunAsCertificate")
pks12_cert = crypto.load_pkcs12(cert)
pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM, pks12_cert.get_privatekey())
# Get run as connection information
for the Azure Automation service principal
application_id = runas_connection["ApplicationId"]
thumbprint = runas_connection["CertificateThumbprint"]
tenant_id = runas_connection["TenantId"]
# Authenticate with service principal certificate
resource = "https://management.core.windows.net/"
authority_url = ("https://login.microsoftonline.com/" + tenant_id)
context = adal.AuthenticationContext(authority_url)
return azure_active_directory.AdalAuthentication(
lambda: context.acquire_token_with_client_certificate(
resource,
application_id,
pem_pkey,
thumbprint)
)
# Authenticate to Azure using the Azure Automation RunAs service principal
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
azure_credential = get_automation_runas_credential(runas_connection)
# Intialize the resource management client with the RunAs credential and subscription
resource_client = azure.mgmt.resource.ResourceManagementClient(
azure_credential,
str(runas_connection["SubscriptionId"]))
# Get list of resource groups and print them out
groups = resource_client.resource_groups.list()
for group in groups:
print(group.name)
Use the following code to list the default installed modules:
#!/usr/bin/env python3 import pkg_resources installed_packages = pkg_resources.working_set installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages ]) for package in installed_packages_list: print(package)
To install a package from the PyPI repository (for example, foo), use the pip install command with the --user flag; for example:,To install a Python package from a source other than the PyPI repository, you can download and unpack the source distribution yourself, and then use its setup.py script to install the package in the user site-packages directory:,The pip package management tool, one of the standard tools maintained by the Python Package Authority (PyPA), is the recommended tool for installing packages from the Python Package Index (PyPI) repository.,The --user option directs setup.py to install the package (for example, foo) in the user site-packages directory for the running Python; for example:
To install Python packages, you must have Python added to your user environment. The IU research supercomputers use module-based environment management systems that provide a convenient method for dynamically customizing your software environment. To check which modules are currently loaded; on the command line, enter:
module list
module load python
module avail python
If Python is listed among the currently loaded modules, but you prefer or need to use another version, you must remove the currently loaded module before loading the other version. To do this with one command, use module switch
; for example, on the command line, enter (replace current_version
with the version number of the currently loaded python
module and new_version
with the preferred version number):
module
switch python / current_version python / new_version
The --user
option directs pip
to download and unpack the source distribution for your package (for example, foo
) in the user site-packages
directory for the running Python; for example:
~/.local/lib / python3 .6 / site - packages / foo
module load python
module avail python
version_number
module load python / version_number
foo-1.0.3.gz
wget http: //pythonfoo.org/foo-1.0.3.gz
foo-1.0.3.gz
tar - xzf foo - 1.0 .3.gz
In Python, we can create tar files using the tarfile module. Open a file in write mode and then add other files to the tar file. The following screenshot shows the files in the folder before creating a zipped file.,We can add extra files into a tar file directly using the add() method from the tarfile module as we have done while creating tar file.,To check the contents of a tar file without extracting them, we can use the getnames() method of the tarfile module. getnames() method returns a list of names of files in the tar file.,Here the open() method takes the filename of the tar file to be created as first argument and “w” for opening the file in write mode. add() method takes the filename of the file to be added to the tar file as an argument.
#import module
import tarfile
#declare filename
filename = "tutorial.tar"
#open file in write mode
file_obj = tarfile.open(filename, "w")
#Add other files to tar file
file_obj.add("plane.xml")
file_obj.add("sample.txt")
file_obj.add("person.ini")
#close file
file_obj.close()
#import module
import tarfile
#declare filename
filename = "tutorial.tar"
#Check
for the file being tarfile
#this will give true
flag = tarfile.is_tarfile(filename)
print("tutorial.tar is a tar file?")
print(flag)
#this will give false
flag = tarfile.is_tarfile("plane.xml")
print("plane.xml is a tar file?")
print(flag)
tutorial.tar is a tar file ?
True
plane.xml is a tar file ?
False
#import module import tarfile #declare filename filename = "tutorial.tar" #open file in write mode file_obj = tarfile.open(filename, "r") # get the names of files in tar file namelist = file_obj.getnames() #print the filenames print("files in the tar file are:") for name in namelist: print(name) #close file file_obj.close()
files in the tar file are: plane.xml sample.txt person.ini
#import module import tarfile #declare filename filename = "tutorial.tar" #open file in append mode file_obj = tarfile.open(filename, "a") # print initial content of tarfile namelist = file_obj.getnames() print("Initial files in the tar file are:") for name in namelist: print(name) file_obj.add("sampleoutput.txt") # print final content of tarfile namelist = file_obj.getnames() print("Final files in the tar file are:") for name in namelist: print(name) #close file file_obj.close()