0

I have followed a wildly accepted answer, which did not work for me. Here is what I have: Folder structure:

Demo folder

enter image description here

Fold1

enter image description here

Fold2

enter image description here

Also, the __init__.py is an empty file. Has nothing in it.

Objective: import customfunction.py file from Demo or Fold1 into Fold2

Code in MainFile.py:

import pandas as pd
import Demo.customfunctions

Output:

ModuleNotFoundError: No module named 'Demo'
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • Put the `Mainfile.py` (or whatever you run initially) into the main directory! – Klaus D. Apr 20 '22 at 03:52
  • @KlausD. you mean in `Demo` folder? The idea was to have the common files in the parent directory, in this case, `Demo` and I can use this in all sub-folders. – Mainland Apr 20 '22 at 04:54
  • The idea of Python is quite the opposite. The folder of the module you run becomes the import base and you can't import above it (without manually adapting the PYTHONPATH). All modules to import go there or below. – Klaus D. Apr 20 '22 at 05:04
  • @KlausD. I looked at one of the highest-voted answers in the link I provided and was following the same. For past couple of years, the issue I am facing is this. I have a list of standard functions and I want to store them in one common `.py` file and share across my programs. This helps me avoid copied of it and helps me develop the main function file. I can update this on `Github` for my personal tracking. Do you have any better solution? – Mainland Apr 20 '22 at 14:33

1 Answers1

1

All you need is a proper combination of your current working directory, the PYTHONPATH environment variable, and the path to the script.

If your current working directory is Downloads, you can run the Demo.Fold2.MainFile module without modifying PYTHONPATH. I.e.

> python -m Demo.Fold2.MainFile

Or you can run the Demo\Fold2\MainFile.py file if you set PYTHONPATH to the current working directory. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=.
> python Demo\Fold2\MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python Demo\Fold2\MainFile.py

If your current working directory is Demo, you can run the Fold2.MainFile module if you set PYTHONPATH to the Downloads directory. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..
> python -m Fold2.MainFile
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python -m Fold2.MainFile

or the Fold2\MainFile.py file. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..
> python Fold2\MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python Fold2\MainFile.py

If your current working directory is Fold2, you can run the MainFile module if you set PYTHONPATH to the Downloads directory. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..\..
> python -m MainFile
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python -m MainFile

or the MainFile.py file. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..\..
> python MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python MainFile.py
radekholy24
  • 418
  • 2
  • 12
  • I am a beginner. If you don't mind, I am not familiar what this `Pythonpath`. I am totally lost it here. I really appreciate it if you could explain. The first question is, do I have to include this python path in my `MainFile.py` ? I tried this but got the error: `PYTHONPATH=/absolute/path/to/Downloads python3 Demo/Fold2/MainFile.py ^ SyntaxError: invalid syntax` – Mainland Apr 21 '22 at 01:06
  • @Mainland, `PYTHONPATH` is an environment variable that augments the default search path for module files (https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH). Environment variables are set outside of your Python files. On Windows, you can either set them from the Command Prompt or via a graphical user interface. Take a look e.g. at the two top answers to this question: https://stackoverflow.com/q/3701646/542196 or at the official documentation: https://docs.python.org/3/using/windows.html#excursus-setting-environment-variables – radekholy24 Apr 21 '22 at 06:22
  • Also please note that you have to adjust the absolute path to `Downloads` according to your environment. It may be something like `C:\Users\Mainland\Downloads`. – radekholy24 Apr 21 '22 at 06:24