1

I would like to load in a function/module with my working directory as project main directory but the function file is store below the a subdirectory level so the normal

from function_file import function_name

does not work.

This is what the project directory set up looks like:

└───project_main_directory
    │   .gitattributes
    │   .gitignore
    │   README.txt
    │
    ├───code
    │   ├───exploration
    │   └───scripts
    │       │   script1.py
    │       │   script2.py
    │       │   script3.py
    │       │
    │       └───functions
    │           │   function1.py
    │           │   function2.py
    │           └───__init__.py
    │
    ├───data
    │   └───example_data
    │           data.csv
    └───documents

So I tried to import functions via

import code.scripts.function.function1 from function1

and that doesn't work. I know it's because the other subdirectories aren't modules, but I want to ask if there is away around that?

-- EDIT I'm working from .py file in code/scripts/script1.py but working directory is project_main_directory/

Oliver Oliver
  • 2,057
  • 4
  • 16
  • 14

3 Answers3

1

Add an empty file __init__.py to every subdirectories to make them as modules.

.
├── code
│   ├── __init__.py
│   └── scripts
│       ├── __init__.py
│       └── script1.py
└── main.py

Then if you have a function called hello in code/scripts/script1.py you can import that function by:

from code.scripts.script1 import hello
hello("yo")
Li Jinyao
  • 898
  • 2
  • 12
  • 30
  • I get error: ```ModuleNotFoundError: No module named 'code.scripts'; 'code' is not a package``` even after added __init__.py to every subdirectory – Oliver Oliver Mar 08 '19 at 05:58
  • @OliverOliver Note it is double _, __init__.py。I‘ve tested it both on Python2 and Python3. Could you check it again in a new project with same folder structure in my answer? – Li Jinyao Mar 08 '19 at 06:05
  • yes i added ```__init__.py``` to each subdirectory and still get same error – Oliver Oliver Mar 08 '19 at 06:09
  • Oh I see you edit. If you are working on code/scripts/script1.py, just `from function import function1` should works. – Li Jinyao Mar 08 '19 at 06:14
  • Yes, but the working directory is ```project_main_directory``` – Oliver Oliver Mar 08 '19 at 06:16
  • 1
    In python, there is no "working directory" to constrain a project's "root". Where the code file is, there is the "working directory" of that code. – Li Jinyao Mar 08 '19 at 06:20
  • OK, I didn't know this. I'm used to R where there is a working directory and that is set the project directory and subdirectories contain code and data but paths are always relative to the main project directory – Oliver Oliver Mar 08 '19 at 06:23
  • My solution was with respect to project_main_directory being your current directory. It depends on the IDE you are using. In Jupyter it is the directory of your .py file. In Spyder you can set your working directory. (similar to R) – Ankit Malik Mar 08 '19 at 17:15
0

If your current directory is project_main_directory, you can use:

from code.scripts.functions.function1 import function1

Directory of your script doesn't matter. Only your current directory matters (refer top of the IDE)

  • I get error ```ModuleNotFoundError: No module named 'code.scripts'; 'code' is not a package``` – Oliver Oliver Mar 08 '19 at 05:55
  • Just to clarify: 'code' is a directory? Python is case sensitive, so in case there are any CAPS please be careful. – Ankit Malik Mar 08 '19 at 05:57
  • My solution was with respect to project_main_directory being your current directory. It depends on the IDE you are using. In Jupyter it is the directory of your .py file. In Spyder you can set your working directory. (similar to R). Your question should have included the IDE you are using. – Ankit Malik Mar 08 '19 at 17:16
0

To import function/module from another python file, you have to do something like below -

from code.scripts.functions.function1 import function1

Above we are loading function1 from function1.py file which is stored in functions directory which is stored in scripts directory and finally in code directory.

EDIT - so you are saying, you want to load a function from function1.py in script1.py? In that case from .functions.function1 import function should work.

  • I get error: ```ModuleNotFoundError: No module named 'code.scripts'; 'code' is not a package``` – Oliver Oliver Mar 08 '19 at 05:55
  • if you are at project_main_directory then, `.code` use (.) dot at the start – Rohit Sinha Mar 08 '19 at 06:02
  • That gives this error ```ModuleNotFoundError: No module named '__main__.code'; '__main__' is not a package``` – Oliver Oliver Mar 08 '19 at 06:06
  • this should work if you are trying to load function/module from the project_main_directory. Also in project_main_directory should have python file where you are loading function/module. – Rohit Sinha Mar 08 '19 at 06:12
  • Oh the python file I'm loading the function in from is NOT in project_main_directory – Oliver Oliver Mar 08 '19 at 06:13
  • so you are saying, you want to load a function from function1.py in script1.py? In that case `from .functions.function1 import function` should work. – Rohit Sinha Mar 08 '19 at 06:19