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

Prerequisites

The version of OSS SDK for Python described 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 the appropriate version of Python, visit the python official website.

    Notice When you install OSS SDK for Python on a computer that runs the Windows operating system, make sure that the version of Visual C++ is 15.0 or later.
  • 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 to add the installation paths of Python and pip. The installation path of pip is the Scripts directory in the installation path of Python.

    Notice 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, the installation is not affected but crcmod extensions in C fail to be installed. In this case, operations such as object upload and download are performed in an inefficient manner. Install python-devel if it does not exist.

  • When you install Python on 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 on CentOS, RHEL, or Fedora systems, run the following command to install python-devel:
    yum install python-devel                 
  • When you install Python on 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 For Python 2.7.9 or later minor versions as well as Python 3.4 or later minor versions, pip is installed by default. If you need to install pip, see Installation.
  • Use the source code

    Click aliyun-oss-python-sdk 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

  • 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 C extension mode and the Python-only mode to calculate the value of CRC. In the C extension mode, the crcmod dependent library is used to call the _crcfunext.so extension library to calculate the CRC value. 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, see crcmod introduction.

    When you use OSS SDK for Python, if the object upload and download speed remains much lower than that of other tools such as ossutil or other SDKs, crcmod extensions in C may fail to be 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 to 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 fail to be installed:
        >>> 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 to fail to be generated. Therefore, the Python-only mode is used to calculate the value of CRC. OSS SDK for Python is installed, but the speed of operations such as object upload and download is slow.

        For Windows, to resolve this issue, download crcmod-1.7.win32-py2.7.msi or other versions of the .msi file and install the SDK. During the installation, specify the installation path of crcmod as the Lib\site-packages directory in the local installation path of the SDK. Example: D:\python\Lib\site-packages\. Verify crcmod after OSS SDK for Python is installed.

        Note The crcmod library for win32 systems is also compatible with win64 systems.

        For Linux, perform the following steps to resolve the issue:

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

          If the crcmod library fails to be installed 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 be installed, uninstall OSS SDK for Python by using pip and then re-install OSS SDK for Python. Run the following command to uninstall OSS SDK for Python:

pip uninstall oss2