0

I am using Python 3.6.0 and having trouble with a project across several directories. The directory structure looks like this:

/project
/project/frontend

All the functionality of my frontend is finished and tested locally and lives in /project/frontend and I now want to connect it with my backend which lives in /project

So I changed into /project/frontend and ran display_page.py which contains the lines:

sys.path.append('../')
from text_algorithms import process_text

..where text_algorithms.py sits in /project and works fine when run from there. So it started running for a good thirty seconds then crashed, complaing that it couldn't find its pickle file which sits in /project There is never a problem when text_algorithms.py is run or imported from /project text_algorithms.py contains the lines:

with open('english_vocab.pickle', 'rb') as f:
    v = pickle.load(f)  

So I thought I could 'fool' it by running python frontend/display_page.py from the directory below but that created a different error, namely,

ModuleNotFoundError: No module named 'text_algorithms'

Does anyone know how to fix this? - how to make sure that it will not change where it looks for that file depending on where you call it from. Is there a proper way to deal with this situation?

It should find its file wherever it is run from.

cardamom
  • 6,873
  • 11
  • 48
  • 102
  • Do you have `__init__.py` in the root and all its subdirectories? – EvgenyKolyakov Jun 27 '17 at 15:58
  • I read you don't need that in Python 3.3+ https://stackoverflow.com/questions/37139786/is-init-py-not-required-for-packages-in-python-3 – cardamom Jun 27 '17 at 15:59
  • If you are going to use `sys.path` hacks, then don't use relative paths: `sys.path.append('../')`. You should probably just add the root directory to your path environment variable. Perhaps inside a virtual environment. – juanpa.arrivillaga Jun 27 '17 at 16:29
  • Thanks, I did not know that was a hack how else are you supposed to import a function from a file in another directory? Looks like I've found a solution [here](https://stackoverflow.com/questions/40416072/reading-file-using-relative-path-in-python-project/40416154#40416154) read the absolute path then `os.chdir` to it but say if I'm using one hack to cancel out another. – cardamom Jun 27 '17 at 16:34
  • @juanpa.arrivillaga I had to add this ridiculously long line `sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir))` from [here](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) in order to make that relative path thing foolproof – cardamom Jun 27 '17 at 18:11
  • @cardamom yep. Which is why you should probably add the root directory to your path variable. – juanpa.arrivillaga Jun 27 '17 at 18:12

1 Answers1

1

You should import other modules of your project:

from frontend.text_algorithms import process_text

If in text_algorithms you want to access files relative to this module then you should use the value of __file__ which contains the file path to the particular module. With os.path you can obtain the directory and join it with the data file path.

For example:

file_path = os.path.join(
    os.path.split(__file__)[0],
    'some.file'
)
a_guest
  • 34,165
  • 12
  • 64
  • 118
  • Thanks I also am thinking something with `os.path` and `__file__` will fix it testing it out – cardamom Jun 27 '17 at 16:37
  • Yes, it works, thanks. Why did it take me 49 minutes to get it to work.. I don't think your import statement with the dot is different to the `import..from` method **but** it was those 4 lines `os.path.join...` in the file of the directory below that did it. I wish you had written something like `dictionary_file =` that thing took me a while to realise those 4 lines were not changing the path but simply creating a string with the correct path to the file, wherever it is called from. – cardamom Jun 27 '17 at 17:30