Can't import pathlib

38,320

Solution 1

Read the docs:

11.1. pathlib — Object-oriented filesystem paths

New in version 3.4.

While a very small number of things released after 3.1 made it back to 2.7, the fact that no pathlib docs exist in the Python 2 docs should be a giveaway.

The os.path module does exist in every version, so import os.path should work just fine.

Solution 2

You may install library pathlib2 by the following command:

pip install pathlib2

After that, your import should work:

import pathlib2 as pathlib

If you want to use just standard modules, usage of os.path is also possible, but it has another functions and working differently.

Share:
38,320
Nonox
Author by

Nonox

Updated on July 09, 2022

Comments

  • Nonox
    Nonox almost 2 years

    Whenever I write:

    import pathlib 
    

    or

    from pathlib import path 
    

    I got this:

    ImportError: No module named pathlib
    

    I tried also naming it os.path.

    I'm on Python 2.7.14

    Is pathlib in Python 2 or only in 3? If it's not available, what else can I use?

  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.