-1

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

user2155362
  • 1,657
  • 5
  • 18
  • 30

1 Answers1

0

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

sys.path.insert(0, str(Path(__file__).parent.parent))
Bibhav
  • 1,579
  • 1
  • 5
  • 18