How to Run Python Script on Terminal

How to run python scripts in another directory, without writing scripts to disk?

you might use pythons os.chdir() function to change the current working directory:

import os
#import methods

root = os.getcwd()

for d in ['subdir1', 'subdir2']:
os.chdir(os.path.join(root, d))
print("dir:", os.getcwd())
exec(open("../driver.py").read())

I am also not sure if you need popen, since python is able to execute python files using the exec function. In this case it depends on how you import the methods.py. Do you simply import it or use it somehow else inside of your driver.py ?

You could try to import it at toplevel inside your main script or use an extended path like:

exec(open("../methods.py").read())

inside of your driver script. Keep in mind these solutions are all not very elegant. Best would be processing the path inside of your driver.py as suggested by Gino Mempin. You could call os.chdir() from there.

How can I run my Python file using PyCharm terminal?

Open the terminal and type:

python code.py

Run function from the command line

With the -c (command) argument (assuming your file is named foo.py):

$ python -c 'import foo; print foo.hello()'

Alternatively, if you don't care about namespace pollution:

$ python -c 'from foo import *; print hello()'

And the middle ground:

$ python -c 'from foo import hello; print hello()'

Python Script execute commands in Terminal

There are several ways to do this:

A simple way is using the os module:

import os
os.system("ls -l")

More complex things can be achieved with the subprocess module:
for example:

import subprocess
test = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)
output = test.communicate()[0]

Run python script with module/ modules already imported

I think what you'r looking for is the interactive mode:

-i
When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command

So just use python -i cls.py in your batch file. This would import your Python file and stay in the Python interpreter prompt. You could then just call cls() because it's already imported.

Alternatively, you could set the environment variable PYTHONSTARTUP in your batch file:

If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.

How to run python code from terminal in VS Code?

I'm sorry to tell you that this should not be possible. After the python script runs, the cache is usually cleaned up after the session ends. So you can't get the data and save it without running the script again.

ModuleNotFoundError when running script from Terminal

You probably want to run python -m products.fish. The difference between that and python products/fish.py is that the former is roughly equivalent to doing import products.fish in the shell (but with __name__ set to __main__), while the latter does not have awareness of its place in a package hierarchy.



Related Topics



Leave a reply



Submit