2

What the title says. I have the following file structure inside the pathC:\Users\User\Desktop\all python file\5.0.8:

5.0.8\
   tree one\
      little main.py
      sample_tree1.py
   tree two\
       sample_tree2.py

the following is what's inside little main.py:

import sys
import sample_tree1

sys.path.insert(0, r'C:\Users\User\Desktop\all python file\5.0.8\tree two\sample_tree2.py')

import sample_tree2

I want to import sample_tree2.py, but an error is raised as soon as I run the little main.py:

Traceback (most recent call last):

  File "<ipython-input-1-486a3fafa7f2>", line 1, in <module>
    runfile('C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py', wdir='C:/Users/User/Desktop/all python file/5.0.8/tree one')

  File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\User\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/User/Desktop/all python file/5.0.8/tree one/little main.py", line 12, in <module>
    import sample_tree2

ModuleNotFoundError: No module named 'sample_tree2'

So what happened? I followed this post's top answer in order to import a file from another branch of path, but it doesn't work.

Thank you in advance


EDIT:

I am looking for a solution that:

1) does not require changing the current working directory

2) does not require changing the names of the files

3) does not require changing the configuration of the python terminal stuff


EDIT2: Added some capscreens of files and folder structures, and error messages

path without sample_tree2.py path with sample_tree2.py enter image description here enter image description here enter image description here

mathguy
  • 1,450
  • 1
  • 16
  • 33
  • Try with directories without spaces in it. – ipaleka Jul 14 '19 at 05:14
  • @ipaleka this requires changing the names of the directory, which is not allowed. But still, even changing the `tree two` to `treetwo`, and uses `import treetwo.sample_tree2`, the error `No module named 'treetwo'` is raised – mathguy Jul 14 '19 at 05:19
  • Sorry, figured out that condition too late. Try with creating os.path.join objects instead of strings as that has been your issue here: take a look at the different way your path is presented in stack trace compared to paths from above. – ipaleka Jul 14 '19 at 05:26
  • @ipaleka can you type them down to the answer so that I can run the code without misinterpreting your reply? – mathguy Jul 14 '19 at 05:30
  • @ipaleka I used `os.path.exists(r'C:\Users\User\Desktop\all python file\5.0.8\treetwo\sample_tree2.py') == True` to check whethere there is a typo in the string of absolute path, turns out I have been correct; the absolute path is right, so the issue does not lie in the string of path – mathguy Jul 14 '19 at 05:40
  • Did you try removing the file name from the path? Then you are inserting an actual path into the path list. – DaveStSomeWhere Jul 14 '19 at 05:44
  • @DaveStSomeWhere what does 'removing the file name from the path' mean? move `sample_tree2.py` to desktop and put it abck under `treetwo` and run `little main.py` again? or `sys.path.insert(0, r'C:\Users\User\Desktop\all python file\5.0.8\tree two'`? I have done both cases and the result still hasn't changed – mathguy Jul 14 '19 at 05:47
  • Is it `treetwo` as per your `path.exists` comment or `tree two` as per your more recent comment? I was talking about case 2, just removing the file name from the `insert` command. This works correctly on ubuntu with py3.7. You can also try `print(sys.path)` to see the path list and on mine, I don't see any file names. – DaveStSomeWhere Jul 14 '19 at 05:58
  • @DaveStSomeWhere I tried both versions of with/without filename in the insert commend(in the pictures I uploaded), they both failed for some reasons. I added more pictures as well to showcase my full folder hierarchy and error messages. – mathguy Jul 14 '19 at 06:26
  • Line 25 on your first pic is trying to import the folder not the file. Also the `os.path.exists` will return true if the path or file exists (not that they are added to the path list you want), so makes sense that both are true, but, you didn't add the path with the `insert`, which you still need to do and can verify with `print(sys.path)` – DaveStSomeWhere Jul 14 '19 at 06:32
  • @DaveStSomeWhere Do you mind typing your answer to the answer below?I think it is better this way, because then I can copy your code and run it and that there will be 0 difference between your code and my code, which makes debugging a whole lot easier – mathguy Jul 14 '19 at 06:43

1 Answers1

1

The sys.path.insert() command inserts a path into the system path and should not contain the filename.

Please try below using your structure:

little main.py:

import sys
import sample_tree1

sys.path.insert(0, r'/my/absolute/path/5.0.8/treetwo')
print(sys.path)  # view the path and verify your insert

import sample_tree2

print(sample_tree2.tree2_func())

sample_tree2.py in treetwo

def tree2_func():
    return 'called tree2'

Outputs:

['/my/absolute/path/5.0.8/treetwo', '... other paths']

called tree2

Community
  • 1
  • 1
DaveStSomeWhere
  • 2,475
  • 2
  • 22
  • 19
  • It works. Before this I tried `import treetwo`, `import treetwo.sample_tree2` and `from treetwo import sample_tree2`, and I somehow managed to miss the most obviosu one. How dumb I really was. Huge thanks for your follow-up – mathguy Jul 14 '19 at 07:02