# Calling multiple Python files from one file

**URL:** <https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484>\
**Category:** FLAC3D\
**Tags:** python\
**Created:** [April 28, 2022, 2:07am UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484 "2022-04-28T02:07:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sb\_guido](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sb\_guido](https://forum.itascainternational.com/u/sb_guido)\
**Post date:** [April 28, 2022, 2:07am UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484/1 "2022-04-28T02:07:42Z")

</div>

Hello,

Instead of having a long .py file with all my commands, how can I have my “MASTER.py” call different files (e.g., ‘densify.py’)?

I am referring to the equivalent of “Program call ‘densify.dat’” but for Python.

Is the syntax for calling another file the same if the file (‘densify.py’) contains user-defined functions or not? If so, what is the syntax?

Typical Python

Thanks!

---

<div class="post-metadata">

**Author:** ![cheng](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@cheng](https://forum.itascainternational.com/u/cheng)\
**Post date:** [April 28, 2022, 3:29am UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484/2 "2022-04-28T03:29:33Z")

</div>

This is a pure Python (rather than FLAC3D) question.

---

<div class="post-metadata">

**Author:** ![sb\_guido](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sb\_guido](https://forum.itascainternational.com/u/sb_guido)\
**Post date:** [April 28, 2022, 4:25am UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484/3 "2022-04-28T04:25:08Z")

</div>

It’s a forum. I think all sort of questions may be asked… Perhaps my question should have been in “General” - tag : “Python”. My appologies if this is the case…

The “from densify import dens\_coarse” command returns this error :

**Python error: ModuleNotFoundError(“No module named ‘densify’”,)**

I don’t have this issue when using Python in PyCharm, Spyder, etc. Seems like it only happens in FLAC3D.

---

<div class="post-metadata">

**Author:** ![cheng](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@cheng](https://forum.itascainternational.com/u/cheng)\
**Post date:** [April 28, 2022, 1:43pm UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484/5 "2022-04-28T13:43:31Z")

</div>

In “a.py”:  
exec(open(“b.py”).read())

will call “b.py”

---

<div class="post-metadata">

**Author:** ![sb\_guido](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@sb\_guido](https://forum.itascainternational.com/u/sb_guido)\
**Post date:** [April 29, 2022, 4:10pm UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484/6 "2022-04-29T16:10:26Z")

</div>

It works, thanks Zhao! I was also able to use this to pass along existing variables.

---

<div class="post-metadata">

**Author:** ![Theophile](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.itascainternational.com/theophile/32/299_2.png) [@Theophile](https://forum.itascainternational.com/u/Theophile)\
**Post date:** [May 13, 2022, 2:25pm UTC](https://forum.itascainternational.com/t/calling-multiple-python-files-from-one-file/484/7 "2022-05-13T14:25:17Z")

</div>

I know it’s an old question, but in case it could help.

My guess is that it’s only a matter of `path` environment variable: when importing a module in Python, it looks in the folder Python is executed from, and in its library folders (the defaults are `%ProgramFiles%/Itasca/FLAC3D/exe64/python36/Lib` and the `site-packages` subfolder in there).

If the module you’re trying to import is not in these, you’ll get an **ImportError**.  
When executing from PyCharm, Spyder…, I guess the _densify.py_ is in the folder you execute Python from. However, the execution folder of FLAC3D might not be this one (default is `%ProgramFiles%/Itasca/FLAC3D/exe64`).

If you wish to import a Python module from anywhere, you have to hardcode the path Python should look into in the script you’re executing (say _MASTER.py_) :

```python
import sys
import pathlib

path_to_densify_headdir = pathlib.Path('path/to/folder/where/densify/is')
sys.path.append(str(path_to_densify_headdir))
import densify

```

Hope it helps.
