0

I’ve been working a lot lately with python 3 and I found that I am unable to import a module from a separate folder. Is there a way to import it while it’s in a folder subdivision? To give more context of this issue here is the “launcher” location and folder that I want to access:

Launcher.py
Folder
- program-to-import.py

That’s the layout. How do I import into launcher from my other module?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Silveradoz
  • 49
  • 1
  • 1
  • 9
  • 1
    Have a look at https://docs.python.org/3/tutorial/modules.html#packages – Thierry Lathuille Jul 21 '18 at 15:30
  • You'd have to start by taking the dashes out of that name. This being python, there is of course a way to import a file with dashes, but it's not something you want to focus on as a beginner. – Mad Physicist Jul 21 '18 at 15:35
  • I've used my close vote, but any of the following are dupes: https://stackoverflow.com/q/5155135/2988730, https://stackoverflow.com/q/391879/2988730, https://stackoverflow.com/q/1801878/2988730 – Mad Physicist Jul 21 '18 at 15:42
  • Possible duplicate of [How to organize a Python Project?](https://stackoverflow.com/questions/5155135/how-to-organize-a-python-project) – Thierry Lathuille Jul 21 '18 at 15:44
  • @MadPhysicist Thanks for the research, I've voted as duplicate of the first one. – Thierry Lathuille Jul 21 '18 at 15:46
  • Possible duplicate of [How to do relative imports in Python?](https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python) – Kieran Lavelle Jul 21 '18 at 16:03

2 Answers2

-1

Python supports importing from child paths rather trivially. in Launcher.py enter the following.

from Folder.program-to-import import *
Kieran Lavelle
  • 446
  • 1
  • 6
  • 22
  • 1/ This wont work if `Folder` isn't a package. 2/ Importing * is usually discouraged. – Thierry Lathuille Jul 21 '18 at 15:33
  • 1
    Also, using - in a package or module name is prohibited for obvious reasons. – Mad Physicist Jul 21 '18 at 15:34
  • @MadPhysicist The name was took from his example, I am well aware that a - should not be used. -ThierryLathuille This does work even if Folder is not a package. Import * is discouraged although the user did not state what the structure of the folder to be imported was so * was used. – Kieran Lavelle Jul 21 '18 at 15:58
  • Thank so for the answer. After a little work I modified all the program and got it working. I have been working with python for a rather short time so I hope you all can understand why I am a little on the unknowledgeable side of things. – Silveradoz Jul 21 '18 at 16:57
-1

As mentioned by others, - in name is invalid, try importing after removing them if you have them in your file name. For now, let's call it program_to_import

from folder import program_to_import

And to call a function from program_to_import, you use this -

program_to_import.function_to_call()

Also, it's always a good idea to look at the documentation

You could also try by adding an __init__.py in your folder. The use of __init.py__ is as follows -

The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later (deeper) on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later.

Sushant
  • 3,499
  • 3
  • 17
  • 34
  • Does this work without adding `__init__.py` to the `folder`? – Kohki Mametani Jul 21 '18 at 15:35
  • 1/ This wont work if Folder isn't a package. 2/ As already noted by @Mad Physicist, `-` is forbidden in names. – Thierry Lathuille Jul 21 '18 at 15:36
  • @ThierryLathuille `-` in names is asked by the OP – Sushant Jul 21 '18 at 15:37
  • N@ThatBird No, if `Folder` doesn't contain a `__init__.py` file (even empty), it won't be considered a package, so importing this way wont work. The filename the OP suggests **can't** be used as it contains `-`, so it's important to tell him. – Thierry Lathuille Jul 21 '18 at 15:38
  • Created two python files in my machine, one in a directory and one in the level of the directory. Called a function from the python file in the directory – Sushant Jul 21 '18 at 15:38
  • @ThatBird. Your job is to explain what OP is doing wrong, not excuse your incorrect code by saying "OP did it". – Mad Physicist Jul 21 '18 at 15:39
  • Also, it's not my **job** now is it? – Sushant Jul 21 '18 at 15:40
  • @ThierryLathuille I understand, and edited the answer – Sushant Jul 21 '18 at 15:41
  • 1
    Not your job per se, but it's presumably the burden you've willingly accepted when you decided to post a good answer :) – Mad Physicist Jul 21 '18 at 15:44
  • For those of you who want more info, I was using the names as an example. Of course I would never try to use - in my names. I am a novice however. If you wish to see the actual program, it is a huge program (or soon to be at least) that I am slowly working on in my free time. For reference, I used launcher to represent `main.py` and I used program-to-import.py as `LevelLoader.py` I also have subfolders and I am going to have to use the `__init__.py` for that. Anyways, here is the link... please don't criticize me on it. https://repl.it/@SilverGaming/DandD-project – Silveradoz Jul 21 '18 at 17:03