0

How does one correctly import a module from a folder that your .py isn't in?

I'm using Python 3.8 and I'm trying to load the module csvread.py in testing.py.

The path to this module is Modules/csv/csvread.py

The command doesn't appear to be: from botmanager.Modules.csv import csvread

The stack trace I get is:

Traceback (most recent call last):
  File "C:/Users/QT/PycharmProjects/botmanager/testing.py", line 1, in <module>
    from botmanager.Modules.csv import csvread
ModuleNotFoundError: No module named 'botmanager'

Project tree: https://i.stack.imgur.com/L3tJq.png

Marcel Doe
  • 57
  • 7
  • Can you share the tree of your project folder. On linux try `tree` command – Mayank Jan 26 '20 at 00:12
  • @Mayank At the moment, I reorganized it to look like this. https://i.imgur.com/sRO0qLM.png Does that image work? – Marcel Doe Jan 26 '20 at 00:13
  • You want to import `csvread.py` in `testing.py`? Please also update your question with python version, your method you tried for importing and stack trace of error occurred. This will help us identify the error – Mayank Jan 26 '20 at 00:15
  • Have a look at this post https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time – Mayank Jan 26 '20 at 00:18
  • Follow this here https://stackoverflow.com/questions/43728431/relative-imports-modulenotfounderror-no-module-named-x – Mayank Jan 26 '20 at 00:22
  • Does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – Mayank Jan 26 '20 at 00:22

1 Answers1

0

To navigate to scripts in a subfolder, do the following:

import Modules.csv.csvread as csvread
Dennis
  • 2,249
  • 6
  • 12
  • Could you explain why this works instead of needing to do what's listed here? https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time – Marcel Doe Jan 26 '20 at 00:28
  • when you do something like `from botmanager.Modules.csv import csvread` you are trying to import a package/ module.. and there can be various reasons for it to fail to load.. path not reachable or it is simply not a python package/ module.. When you do `import Modules.csv.csvread as csvread` you are importing the file as script. – Mayank Jan 26 '20 at 00:32
  • @Mayank I see. Is from botmanager.Modules.csv import csvread the proper way to import a module? – Marcel Doe Jan 26 '20 at 00:34
  • Its pythonic way to do it. Just make casvread.py a proper module. Try adding `__init__.py` to parent folder. Also check sys.path if `botmanager.Modules.csv` is accessible or not – Mayank Jan 26 '20 at 00:36
  • The `from x import y` statement has to import `x`, then gets `y` from it. You can't import botmanager.Modules.csv because it's not a package--it's just a folder. You could turn it into a package by adding an empty `__init__.py` file in that csv folder. – Dennis Jan 26 '20 at 00:37
  • Thanks for clearing that up. Can I do something like: from Modules.csv import csvread as csv_reader – Marcel Doe Jan 26 '20 at 00:40
  • Only if Modules/csv has an `__init__.py` file (even if the file is empty). – Dennis Jan 26 '20 at 00:49
  • @Dennis Thanks. Let's say I had multiple classes in my Modules/csv/csvread.py -- would the proper way to init it in the main script be: name = csvread.Example() ? – Marcel Doe Jan 26 '20 at 01:00
  • Yes, that should be good. If you really felt like it, you could also `from Modules.csv.csvread import Example; name = Example()`, but the way you suggested might be preferred, depending on the context--explicit is better than implicit. – Dennis Jan 26 '20 at 01:05
  • @Dennis could you explain why ones implicit and the others explicit? – Marcel Doe Jan 26 '20 at 02:18
  • If you're unfamiliar with someone's code and you see "csvread.Example", you explicitly know that the related source code is in the "csvread" module. If you just see "Example", you have to wonder where that name was defined. Sometimes though, it can be clear from context where Example came from, in which case using the `from ... import ...` is fine. – Dennis Jan 26 '20 at 02:23