1

I have 2 folders:

my_python
     code.py

MyCode
     TestEntry.py

When I run the following commands:

cd /data/my_python
python3 code.py

The above works.

However, if I in my home folder and then run this:

python3 /data/my_python/code.py

I get the following error:

Traceback (most recent call last):
  File "/data/my_python/code.py", line 4, in <module>
    from TestEntry import TestEntry
ImportError: No module named 'TestEntry'

Here is the code:

import sys
import os
sys.path.append(os.path.abspath('../MyCode'))
from TestEntry import TestEntry
TestEntry().start(507,"My Param1","/param2",'.xyz',509)

Can you help me how to fix this?

dang
  • 2,342
  • 5
  • 44
  • 91

2 Answers2

1

You are adding a relative path to sys with your line sys.path.append(os.path.abspath('../MyCode')). Instead, you need to import relative to that file you are calling. Try this:

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from TestEntry import TestEntry
TestEntry().start(507, "My Param1", "/param2", '.xyz', 509)
Özer
  • 2,059
  • 18
  • 22
  • But I need to go to another folder to get TestEntry which is /data/MyCode – dang Apr 29 '20 at 17:57
  • Yeah, doing `os.path.dirname` twice goes back 2 folders. If that doesn't work, try with only one. The key here is the `__file__` that you pass into it. It's the path to the file that runs this code. From there, you need to see how many levels you need to go back so python can find your module. – Özer Apr 29 '20 at 17:59
  • Do I need to add 2 lines: sys.path.append(os.path.dirname(os.path.dirname(__file__))) and sys.path.append(os.path.abspath('../MyCode')) – dang Apr 29 '20 at 18:05
  • No, just the first one `sys.path.append(os.path.dirname(os.path.dirname(__file__)))` or `sys.path.append(os.path.dirname(__file__))` – Özer Apr 29 '20 at 18:07
0

That happens because, as @mkrieger1 mentioned, your sys.path gets messed up. I have a previous answer here which explains how to set it. By sys.path getting messed up, I mean that python will look in the dir that you are running from, not the dir that the script you are running is in. Here is the recommended method:

import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'MyCode')))
... (your code)

or

import sys, os
sys.path.append(os.path.abspath(os.path.join(__file__, '..', 'MyCode')))
... (your code)

This way python will look in the dir of the file you are running as well.

xilpex
  • 3,097
  • 2
  • 14
  • 45
  • It doesn't seem to work. Should I add both the lines -- sys.path.append(os.path.dirname(os.path.dirname(__file__))) and sys.path.append(os.path.abspath('../MyCode')) – dang Apr 29 '20 at 18:05
  • @dang -- Maybe. If you gave a better file structure explanation, we could be of better help. – xilpex Apr 29 '20 at 18:08
  • I have added folder explanation in the question. – dang Apr 29 '20 at 18:13
  • For some reason, both didn't work, giving the same error – dang Apr 29 '20 at 18:17
  • I am in my home folder - `cd ~` and ran `python3 /data/my_python/code.py` – dang Apr 29 '20 at 18:19