How to run python file from another directory
Solved see my answer below for anyone who might find this helpful. Show
I have two scripts a.py and b.py. In my current directory "C:\Users\MyName\Desktop\MAIN", I run > python a.py. The first script, a.py runs in my current directory, does something to a bunch of files and creates a new directory (testA) with the edited versions of those files which are simultaneously moved into that new directory. Then I need to run b.py for the files in testA. As a beginner, I would just copy and paste my b.py script into testA and execute the command again "> python b.py", which runs some commands on those new files and creates another folder (testB) with those edited files. I am trying to eliminate the hassle of waiting for a.py to finish, move into that new directory, paste b.py, and then run b.py. I am trying to write a bash script that executes these scripts while maintaining my hierarchy of directories.
Script a.py runs smoothly, but b.py does not execute at all. There are no error messages coming up about b.py failing, I just think it cannot execute because once a.py is done, b.py does not exist in that NEW directory. Is there a small script I can add within b.py that moves it into the new directory? I actually tried changing b.py directory paths as well but it did not work. For example in b.py:
I changed mydirs to mydir_new in all instances within b.py, but that also made no difference...I also don't know how to move a script into a new directory within bash. As a little flowchart of the folders:
TLDR: How do I execute a.py and b.py from the main test folder (bash script style?), if b.py relies on files created and stored in testA. Normally I copy and paste b.py into testA, then run b.py - but now I have 200+ files so copying and pasting is a waste of time.
There are two ways you may want to run a Python script located in another folder – as a regular script or import and run it as a module. This article focuses on how we can do this. Contents
Files and Directory StructureWe will use a file named runScript1.py to run two other files, script1.py and module1.py, located in another directory. We will execute script1.py as a regular Python script and module1.py as a module. Here is the directory structure (it’s recommended you create these files in the tree structure shown and follow along), We will keep editing runScript1.py, but the content of the other two files will remain the same throughout this post. It is as follows: script1.py:
This script has two functions to perform simple mathematical operations – add and multiply. We call multiply() function inside __name__ == ‘__main__’ and add() function outside. Any code inside __name__ == ‘__main__’ is executed only when the script is executed as a regular Python file. If the script is imported as a module, the code under if-block will not be executed. module1.py:
The module1.py has one class (BasicOperations()) with several functions performing different basic mathematical operations. Executing Python Script on Terminal/ShellIn this article, we will consistently execute the runScript1.pyfile on the terminal by running the command “python3 script_name”. You may run into the following error: Just change “Python3” to “Python” in every command and script, so it looks like this: “python script_name”. This assumes that you are on the directory containing the script. To change the directory to the folder containing the script, run “cd Note: In this post, we will use the dollar sign ($) to show that we are running the script on the terminal, for example, $ python3 runScript1.py. Running Python Script in another Directory as Regular ScriptPython script can be used to execute another script on a different directory with the help of either the os or subprocess module. Using these modules to run Python files is equivalent to running a Python script on the shell/terminal. The general syntax used is
Note: If you have Python 2 and Python 3 installed on your computer and want to run Python 2 specify that. Let’s now have the following code in our runScript1.pyand use it to execute script1.py runScript1.py:
After you run the script, you will get this message: Output: ###os module### 7+5 is equal to 12 3*5 is equal to 15 ###subprocess package### 7+5 is equal to 12 3*5 is equal to 15 Notice that the code under __name__ == ‘__main__’ in script1.py is executed. This is because we run the script from runScript1.py as a regular script. To make the paths shorter, you can also use os.chdir() to change the current working directory (this is equivalent to using cd in the command line) and then pass the script names only as shown below runScript1.py:
Run the code. Output: ####os#### 7+5 is equal to 12 3*5 is equal to 15 ####subprocess#### 7+5 is equal to 12 3*5 is equal to 15 Running Python Script in another Directory as a ModuleWhenever you import a module (which is just a Phyton file) in Python by writing “import module1“, for example, the interpreter will look for module1.py in the following places.
If you attempt to import a module that is not available in the above locations, Python will throw ModuleNotFoundError. We can use the sys package to check and modify folders to look for. sys.path shows a list of directories the Python interpreter searches whenever a module is being imported. runScript1.py:
Run the script. Output: ['/home/kiprono/Desktop/test2', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/kiprono/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages'] Those are the directories Python will be looking for whenever you import modules. By default, the current directory is also searched. In our case, it is the first element of the list (that is where runScript1.py is located). If we attempt to import module1.py, at this point, we will get ModuleNotFoundError because the folder containing it (‘/home/kiprono/Desktop/test1’) is not part of the sys.path. Therefore, we must learn how to add a path to the Python environment. Adding a path into the Python environmentThere are two methods – using the sys module or editing Python environment variables explicitly (we will see how to do this in Linux, Mac, and Windows). Method 1: Using the sys packageThe function sys.path.append() allows us to add a path into the environment. Let’s use it to add ‘/home/kiprono/Desktop/test1’, then we can import the module1.py script as a valid Python module. runScript1.py:
Run the script. Output: ['/home/kiprono/Desktop/test2', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/kiprono/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/home/kiprono/Desktop/test1'] 6 As you can see, ‘/home/kiprono/Desktop/test1‘ has been added to the list of directories the interpreter will search, and therefore, we can import module1 and then load the BasicOperations() class. As said earlier, since module1.py is executed as a module, the code under __name__ == ‘__main__’ is not executed. Every time we start another Python script and need to import modules, we have to append the directories to look for. To avoid having to do this, we can add the path(s) into the environment variable. We will see how to do that in Linux, macOS, and Windows. Method 2a: Adding path to an environment variable (on Linux and Mac)On Linux/Mac, you can check the current value of the variable by running this command on terminal
That would be blank unless it has been modified before. To add the path containing our module, we need to: Open .bash_profile (on Mac) or .profile (on Linux) (the file should be in the home folder(~)) and add the following line to the file:
Alternatively, you can run the following command on the terminal to append the line: Linux:
macOS:
If you are running your code on the terminal, you have to restart the terminal (or run source ~/.profile) for the changes to take effect, but if you are running the code on an editor, you might need to reboot the whole system. runScript1.py: Figure 2: Running the script before and after adding the path into the environment.Before adding a path into the environment, Python throws ModuleNotFoundError, but after adding the path, the code on runScript1.py was executed with no error. You can add multiple paths using the line:
If you want to remove a path (say dir2), you can run the following command or remove the path on the .profile file:
Method 2b: Adding path to an environment variable (on Windows)For Windows (using Windows 11), follow these steps:
How do I run a Python file in another directory?To make Python scripts runnable from any location under Windows:. Create directory to put all your python scripts in. ... . Copy all your python scripts into this directory.. Add the path to this directory in Windows "PATH" system variable: ... . Run or restart "Anaconda Prompt". Type "your_script_name.py". How do I open a Python file in D drive?In the navbar of the folder that your file exist, write CMD and hit Enter.. Open Command Prompt (Click on start, write command, press enter). Navigate to your script's folder (e.g. cd your_script_folder). Run your script using the python command (e.g. python your_script_name.py --x 10.1 --y 20.2). |