0

I'm trying to import many other files into another python file but for some reason it's not finding the python files. I've looked at many questions and answers and tried to follow them but I still have no luck.

The directoory looks like this

my_dir/
       one_dir/
             _init_.py
             script_1.py
       parse_lib/
             _init_.py
             hi.py

(Here I am running script_1.py)

import os, sys, re
import subprocess
import multiprocessing as mp

curpath = os.getcwd()
curdir = os.path.dirname(curpath)
newdir = os.path.join(curdir,"/parse_lib")
sys.path.append(newdir)
print(newdir)

import hi

ImportError: No module named hi

To add: I am not running the python file from the directory where the file resides, as in I'm running it from a completely different directory and not in my_dir/one_dir

George
  • 137
  • 1
  • 17

2 Answers2

1

This:

os.path.join(curdir, "/parse_lib")

..gives you "/parse_lib". See why here.

What you're looking for is:

os.path.join(curdir, "parse_lib")

...because the parse_lib folder is not located at the root of your system (/).

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • I fixed that, thank you. I still get the same problem though. – George Jul 09 '19 at 17:52
  • I realized I was missing something. I am not running the python file from the directory where the file resides. I believe now the issue is that my "curdir" is not the one of the strict, but the one of where I am running the script – George Jul 09 '19 at 17:58
0

Following Ronan's answer, I was able to find the solution.

Instead of pulling the directory of the file, I was pulling the working directory. The fix was the following:

curdir = os.path.dirname(__file__)
curdir1 = os.path.dirname(curdir)
newdir = os.path.join(curdir1,"parse_lib")
sys.path.append(newdir)
George
  • 137
  • 1
  • 17