Import Package Python

To import and use a Python package in your script or project, you typically use the import statement. Here’s how you can import a package and use its modules or functions.

1)In your Automation account, select Python packages under Shared Resources. Then select + Add a Python package.

2)On the Add Python Package page, select a local package to upload. The package can be .whl or .tar.gz file for Python 3.8 and .whl file for Python 3.10 (preview).

3)Enter a name and select the Runtime Version as Python 3.8 or Python 3.10 (preview).

4)Select Import: After a package has been imported, it’s listed on the Python packages page in your Automation account. To remove a package, select the package and select Delete.

Importing a Package
Let’s assume you have a package named my_package with the following structure:

lua
Copy code
my_package/
|– my_package/
| |– init.py
| |– module1.py
|– setup.py
Importing the Entire Package
python
Copy code
import my_package

Now you can use functions or modules from my_package

my_package.module1.some_function()
Importing a Module from a Package
python
Copy code
from my_package import module1

Now you can use functions from module1 directly

module1.some_function()
Importing a Specific Function from a Module
python
Copy code
from my_package.module1 import some_function

Now you can use some_function directly

some_function()
Importing with an Alias
You can also use an alias to make your code more readable:

python
Copy code
import my_package.module1 as m1

Now you can use m1.some_function() instead of my_package.module1.some_function()

m1.some_function()
Using a Package Installed via pip
If your package is published on PyPI, you can install it using pip:

bash
Copy code
pip install my_package
After installing, you can import and use it as mentioned above.

Keep in mind that the package needs to be installed or be in the same directory as your script or project for it to be importable. Also, ensure that the necessary dependencies mentioned in the install_requires section of the package’s setup.py are installed.

Note: Replace my_package and module1 with the actual names of your package and modules.

Import a package with dependencies

You can import a Python 3.8 package and its dependencies by importing the following Python script into a Python 3.8 runbook. Ensure that Managed identity is enabled for your Automation account and has Automation Contributor access for successful import of package.

Python training in hyderabad

contact

Enquiry Now
close slider
Scroll to Top
Call Now Button