# Run python script in subfolder

**URL:** <https://discuss.python.org/t/run-python-script-in-subfolder/17584>\
**Category:** Python Help\
**Created:** [July 22, 2022, 9:24am UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584 "2022-07-22T09:24:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ardeal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ardeal/32/4101_2.png) [@ardeal](https://discuss.python.org/u/ardeal)\
**Post date:** [July 22, 2022, 9:24am UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584/1 "2022-07-22T09:24:31Z")

</div>

Hi,

Please check the following picture which explains my questions.  
In Linux, in `Top Folder`, if I run `python Scripts/SC1.py`, the error is:`ModuleNotFoundError: No module named 'package1'`

if I create a soft link in `Top Folder` using command: `ln -s Scripts/SC1.py SC1.py`, and then run command: `python SC1.py`, I still experience the same error.

I don’t want to mv SC1.py to `Top Folder`, how could I run it in Linux?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/2X/f/f93fdc77775911801196abd7fb7ce78f12ebc994.png)

---

<div class="post-metadata">

**Author:** ![Mholscher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mholscher/32/15325_2.png) [@Mholscher](https://discuss.python.org/u/Mholscher)\
**Post date:** [July 22, 2022, 11:57am UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584/2 "2022-07-22T11:57:51Z")

</div>

> [@ardeal](#):
>
> I don’t want to mv SC1.py to `Top Folder`, how could I run it in Linux?

Why not? It is the standard way of doing things.

When you run a script, [the directory of the script is added to the PYTHONPATH](https://stackoverflow.com/questions/6416424/why-does-my-python-not-add-current-working-directory-to-the-path), not the current directory. So anything below `Top Folder`, but not below `Scripts`, becomes unreachable for the loader.

You can of course add the folder to the path, but I would put a script (could be just a small bootstrap) into the `Top Folder` to start your program.

---

<div class="post-metadata">

**Author:** ![frostming](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/frostming/32/326_2.png) [@frostming](https://discuss.python.org/u/frostming)\
**Post date:** [July 22, 2022, 12:42pm UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584/3 "2022-07-22T12:42:11Z")

</div>

There are several options, listing from most to least recommended:

1. Make a caller script under the top folder, that wraps the scripts under `Scripts`. You can import members into the caller script
2. Run as a module: `python -m Scripts.SC1`, although it works, it doesn’t look good because `Scripts` isn’t a module.
3. Add the top-level path to the `sys.path` in each scripts under `Scripts` folder. It is highly not recommended but provided as an option.

---

<div class="post-metadata">

**Author:** ![ardeal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ardeal/32/4101_2.png) [@ardeal](https://discuss.python.org/u/ardeal)\
**Post date:** [July 22, 2022, 1:03pm UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584/4 "2022-07-22T13:03:32Z")

</div>

@Mholscher  
@frostming  
Thank you for your help!

I created a soft link in Linux `ln -s Scripts/SC1.py SC1.py`, and open `Top Folder` in VSCode in Windows 10.  
I could run the soft link `SC.py` in VSCode.  
My question is:  
how could vscode run soft link `SC1.py` in `Top Folder` but the soft link couldn’t be run in Linux terminal?

---

<div class="post-metadata">

**Author:** ![Mholscher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mholscher/32/15325_2.png) [@Mholscher](https://discuss.python.org/u/Mholscher)\
**Post date:** [July 23, 2022, 2:57pm UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584/5 "2022-07-23T14:57:54Z")

</div>

> [@ardeal](#):
>
> My question is:  
> how could vscode run soft link `SC1.py` in `Top Folder` but the soft link couldn’t be run in Linux terminal?

I am really not in a position to tell. There are three participants here: the OS, Python and VS Code. I wouldn’t even know where the culprit would be.

**Wild guess**  
Probably in the terminal Python finds out where the file executing ( `Scripts/SC1.py`) is located and adds that folder to the path 😦 while VS Code adds the current directory to the path.

Option 1 and 2 that @frostming suggested seem both fine to me. Go with one of those.

---

<div class="post-metadata">

**Author:** ![mlgtechuser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mlgtechuser/32/7310_2.png) [@mlgtechuser](https://discuss.python.org/u/mlgtechuser)\
**Post date:** [July 23, 2022, 3:06pm UTC](https://discuss.python.org/t/run-python-script-in-subfolder/17584/6 "2022-07-23T15:06:37Z")

</div>

> [@ardeal](#):
>
> how could vscode run soft link `SC1.py` in `Top Folder`

VS Code has its own `Workspace` environment. I suspect that the Workspace is providing a `path` in addition to your OS’s `path`.
