Shining Zhong
Shining Zhong

Reputation: 336

ImportError: No module named path

I have this line in my script:

from path import Path

but I ran into this error

ImportError: No module named path

however, I have install path.py in my system, there is no problem when I write in python3 with from path import Path in terminal. this is very weird.

Upvotes: 9

Views: 30113

Answers (4)

Ehsan88
Ehsan88

Reputation: 3791

This is somehow funny. But sometimes when you have copied a code from SO or other places, they use this as an illustrative example: path.to.yourpackage. For example mine was

DATABASE_ROUTERS = ['path.to.DatabaseRouter']

So in these cases it's obvious there is no module named 'path'. But you rushed when copying it and forgot fixing it. Posted this so that it helps someone else who may encounter this.

Upvotes: 0

Rexcirus
Rexcirus

Reputation: 2917

Depending on your needs, also

from os import path

works.

Upvotes: 1

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

This should do it:

sudo pip install path.py

Upvotes: 6

falsetru
falsetru

Reputation: 369274

If you mean Path in standard library, use pathlib.Path, not path.Path.

>>> from pathlib import Path
>>> Path
<class 'pathlib.Path'>

Upvotes: 5

Related Questions