How can I import .py file?

Question:

Below is my code:

from pathlib import Path
import os
import sys
sys.path.insert(0, os.path.join(Path(__file__).parent.parent.parent.parent,'public'))
import utilities 
sys.path.insert(0, Path(__file__).parent.parent)
print(Path(__file__).parent.parent)
import publicmethods

I’m tring to import .py file in special directory. I can import utilities.py in os.path.join(Path(__file__).parent.parent.parent.parent,'public') but can’t import publicmethods.py. I’m sure the publicmethods.py under the Path(__file__).parent.parent directory.
What’s wrong with it? Thanks!

ps: my dir structure like below:

c:---
      |--projects
            |---spider
                   |---public
                   |      |---utilities.py
                   |---website
                          |--website
                                |-- publicmethods.py
                                |-- spiders
                                       |-- myspider.py

The code about import write in myspider.py

Asked By: user2155362

||

Answers:

You must convert it to str before inserting it in sys.path:

sys.path.insert(0, str(Path(__file__).parent.parent))
Answered By: Bibhav
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.