15

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?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Nonox
  • 167
  • 1
  • 1
  • 4
  • 2
    The pathlib module was added sometime in python 3. If you want to use it in python 2 you have to install it: `pip install pathlib` – Aran-Fey Apr 08 '18 at 17:51
  • Thanks for your answer ! Could you please explain to me how do I do that ? – Nonox Apr 08 '18 at 17:53
  • 1
    ...you paste `pip install pathlib` into a terminal and press Enter. If it doesn't work, you google the error message. – Aran-Fey Apr 08 '18 at 17:57
  • Sorry if I sound idiot, I'm really just beginning. There is no error message per say, but I got a syntax error for "install"... I don't really get it – Nonox Apr 08 '18 at 18:05
  • 1
    @Nonox: Not in a Python interactive session, in a regular `cmd.exe` (Windows) or `bash` (UNIX-like) terminal. – ShadowRanger Apr 08 '18 at 21:35

3 Answers3

15

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.

Community
  • 1
  • 1
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
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.

  • 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](/help/how-to-answer). – Community Nov 12 '21 at 00:44
0

"P" must be capital the code must be

from pathlib import Path 

not

from pathlib import path
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34535507) – dpapadopoulos Jun 16 '23 at 10:23