0

I am trying to inspect the modules which are loaded from my script and their respective locations that is why I am trying to run

python -v mysrcipt.py
python -vv mysrcipt.py

Since I want to compare how the imports are loaded in two different scripts I want to redirect the verbose output which looks like this

# code object from '/usr/local/lib/python3.5/dist-packages/botocore/__pycache__/translate.cpython-35.pyc'
import 'botocore.translate' # <_frozen_importlib_external.SourceFileLoader object at 0x7f182affa748>
import 'botocore.handlers' # <_frozen_importlib_external.SourceFileLoader object at 0x7f182afe0a90>
# trying /usr/local/lib/python3.5/dist-packages/botocore/loaders.cpython-35m-x86_64-linux-gnu.so
# trying /usr/local/lib/python3.5/dist-packages/botocore/loaders.abi3.so
# trying /usr/local/lib/python3.5/dist-packages/botocore/loaders.so
# trying /usr/local/lib/python3.5/dist-packages/botocore/loaders.py

to a file. To do this I have tried

python -v mysrcipt.py > file.txt
python -v mysrcipt.py 2>&1 > file.txt

but the only thing that is written to the file are the logs of the script and not the verbose output regarding the imports.

How can I successfully redirect the imports related output to a file?

Niko
  • 616
  • 4
  • 20

1 Answers1

1

the > only write STDOUT to the file (changed the standard output location)

if you want write all of the outputs (like stdout, stderr and etc) in to file use this command and pipe (|) all of them to tee:

python3 -v main.py  2>&1 | tee file.txt

or

python3 -v main.py  > output.txt 2>&1

1 for stdout and 2 for stderr.

only write stdout:

python3 -v main.py 1> output.txt

only write stderr:

python3 -v main.py 2> output.txt

both stdout and stderr:

python3 -v main.py > output.txt  2>&1

So when you use 2>&1 you are basically saying Redirect the stderr to the same place we are redirecting the stdout. And that is why we can do something like this to redirect both stdout and stderr to the same place

more info:

How do I save terminal output to a file?

How to redirect output to a file and stdout

PersianMan
  • 924
  • 1
  • 12
  • 29