This topic describes how to install Object Storage Service (OSS) SDK for Python.

Preparations

The version of OSS SDK for Python in this topic is compatible with Python 2.6, 2.7, 3.3, 3.4, 3.5, 3.6, 3.7, and 3.8.

  • Environment requirements

    For more information about how to install an appropriate version of Python, visit python.

  • Check the Python version

    Run the python -V command to check the Python version.

    In Windows, if a message appears, which indicates that it is not an internal or external command, edit the Path environment variable. Add the installation path of Python and pip. The installation path of pip is the Scripts directory in the installation path of Python.

    Note You may need to restart your computer to ensure that the environment variables take effect.

Download OSS SDK for Python

For more information, see API Reference.

Install python-devel

OSS SDK for Python uses the crcmod library to calculate the value of cyclic redundancy check (CRC). The crcmod library depends on the Python.h file. If the Python.h file is missing, SDK installation is not affected but crcmod extensions in C fail to installed. Therefore, the efficiency of operations such as object upload and download is low. Install python-devel if it does not exist.

  • When you install Python in Windows or Mac OS X systems, the Python.h file is installed together with Python. Therefore, you do not need to install python-devel.
  • When you install Python in CentOS, RHEL, or Fedora systems, run the following command to install python-devel:
    yum install python-devel
                        
  • When you install Python in Debian or Ubuntu systems, run the following command to install python-devel:
    apt-get install python-dev
                        

Install OSS SDK for Python

  • Use pip

    Run the following command:

    pip install oss2
                        
    Note If pip is not installed in your environment, visit the official pip website to install pip.
  • Use the source code

    Click here to visit GitHub and download the OSS SDK for Python package of an appropriate version. Decompress the package. Check whether the setup.py file exists in the directory and then run the following command:

    python setup.py install
                        

Verify the SDK version

  • Verify the version of OSS SDK for Python
    1. Enter python in the command line and press the Enter key to go to the Python environment.
    2. Run the following command to verify the version of OSS SDK for Python:
      >>> import oss2
      >>> oss2.__version__
      '2.0.0'
                                  

      If an output similar to the preceding example appears, OSS SDK for Python V2.0.0 is installed.

  • Verify crcmod

    OSS SDK for Python uses the following methods to calculate the value of CRC: the C extension mode, which uses the crcmod dependent library to call the _crcfunext.so extension library to calculate the CRC value, and the Python-only mode. The performance of the C extension mode is superior to the Python-only mode. The crcmod library is automatically installed together with oss2. For more information about crcmod, visit crcmod introduction.

    When you use OSS SDK for Python, if the object upload and download efficiency remains much lower than that of other tools such as ossutil or other SDKs, crcmod extensions in C may fail to installed. In this case, the Python-only mode is used to calculate the value of CRC for object upload and download.

    You can use the following methods to determine whether the C extension mode of the crcmod library is installed:

    1. Enter python in the command line and press the Enter key to go the Python environment.
    2. Enter import crcmod._crcfunext and press the Enter key.
      • If no error message appears, the crcmod extensions in C are installed.
      • If the following error message appears, crcmod extensions in C failed to install:
        >>> import crcmod._crcfunext
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        ImportError: No module named _crcfunext
                                            

        When you compile the crcmod library, the _crcfunext.so file depends on the Python.h file. The error occurs because the Python.h file does not exist in the system, which causes the _crcfunext.so file failed to be generated. Therefore, the Python-only mode is used to calculate the value of CRC. OSS SDK for Python is installed, but the efficiency of operations such as object upload and download is low.

        If the error occurs, perform the following steps:

        1. Run the following command to uninstall the crcmod library:
          pip uninstall crcmod
        2. Install python-devel
        3. Run the following command to re-install the crcmod library:
          pip install crcmod

          If the crcmod library fails to install after you perform the preceding steps, uninstall the crcmod library. Run the following command to view the detailed causes of the installation failures.

          pip install crcmod -v

Uninstall OSS SDK for Python

If OSS SDK for Python fails to install, uninstall OSS SDK for Python by using pip and re-install OSS SDK for Python. Run the following code to uninstall OSS SDK for Python:

pip uninstall oss2