huy
huy

Reputation: 1914

Import many python files in bash script

I have a problem with the import file in the bash script.

My project directory looks like this:

project
├── folder1
│   └── file1.py
├── folder2
│   └── file2.py
├── data
│   └── example.txt
├── utils
│   ├── __init__.py
│   └── globals.py
└── run.sh

The globals.py file contains some config global variables that will be used in the whole project.

I want to write a bash file to run all .py files like this run.sh:

conda activate pyenv

# Step 1.1
python folder1/file1.py data/example.txt >> data/output_1_1.txt

#  Step 1.2
python folder2/file2.py data/output_1_1.txt >> data/output_1_2.txt

But then I got this error:

Traceback (most recent call last):
  File "folder2/file2.py", line 15, in <module>
    from utils import globals
ModuleNotFoundError: No module named 'utils'

This is my file2.py where I import utils.globals:

import sys

sys.path.append("../")  # Add "../" to utils folder path
from utils import globals

When I run each file individually, it works fine, but I don't know why it doesn't work when I run source run.sh.

Upvotes: 0

Views: 121

Answers (1)

huy
huy

Reputation: 1914

As this answer points out it is not good to use a relative path when the project structure is complex.

So I change my project directory to this to make it a package like @bigbounty's suggested:

project
├── package
│   ├── folder1
│   │   └── file1.py
│   ├── folder2
│   │   └── file2.py
│   ├── data
│   │   └── example.txt
│   └── utils
│       ├── __init__.py
│       └── globals.py
└── run.sh

In the file2.py, change the import to this:

from package.utils import globals

The run.sh file:

conda activate pyenv

# Step 1.1
python -m package.folder1.file1 package/data/example.txt >> package/data/output_1_1.txt

#  Step 1.2
python -m package.folder2.file2 package/data/output_1_1.txt >> package/data/output_1_2.txt

To run the bash script, cd to the /project:

(base) user@user1:~/project$ source run.sh

Then it works as expected.

Upvotes: 1

Related Questions