0

I have a directory called NHOME. In this directory there is a folder named subFolder. It's like this:

/NHOME/subFolder

In NHOME directory I'm executing a bash script which intends to execute a python code located in subFolder. I have problem doing this. I tried something like this:

./subFolder/file.py

but it didn't work. Is this possible?

2
  • Have you tried python ./subFolder/file.py? Commented Mar 23, 2020 at 14:49
  • Yes, I got this error: ImportError: No module named parse, while it works when I execute it inside subFolder. Commented Mar 23, 2020 at 14:52

1 Answer 1

1

Use pwd to get the script's location instead of the relative ./

pythonScript="$PWD/subFolder/file.py"

To run:

#!/bin/bash
"$PWD"/subFolder/file.py

Note: The " around $PWD are needed if the path contains spaces ()

Sign up to request clarification or add additional context in comments.

10 Comments

The working directory is available in the variable PWD. This gives the directory where the script itself is stored, which may or may not be the same as the working directory.
$PWD is more efficient than $(pwd) -- the former is just a variable lookup inside the shell instance that's already running, the latter forks off a child process and reads its stdout.
How should I run this inside the script now? PWD/subFolder/file.py? The python code is not supposed to return anything.
The $ is not optional. Anyhow, "$PWD"/subFolder/file.py will work if this answer is correct (and Python does module lookups relative to a source file given as an absolute path).
@0stone0, not ${pwd}, $PWD. The variable is all-caps, which puts it in the namespace POSIX reserves for use of the shell and other POSIX-defined tools, whereas other names are guaranteed not to have unintended conflicts on the behavior of standard-defined tools when used by applications for their own purposes. See pubs.opengroup.org/onlinepubs/9699919799/basedefs/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.