Running an Nsight Systems report python script independently - python

I've tweaked a copy of one of the Nsight Systems report scripts (gpukernsum), and I now want to run it myself. So, I write:
./gpukernsum.py report.sqlite
This doesn't work; I get:
ERROR: Script 'gpukernsum.py' encountered an internal error.
$ ./gpukernsum.py report.sqlite
File "./gpukernsum.py", line 40
"""
^
SyntaxError: invalid syntax
I know this is because f"""whatever""" is Python-3 syntax, so I change the script's hash-bang line from:
#!/usr/bin/env python
to:
#!/usr/bin/env python3
and now I get:
$ ./gpukernsum.py report.sqlite
Traceback (most recent call last):
File "/path/to/./gpukernsum.py", line 7, in <module>
import nsysstats
ModuleNotFoundError: No module named 'nsysstats'
So I added the relevant directory to the lookup path:
export PYTHONPATH="$PYTHONPATH:/opt/nvidia/nsight-systems/2022.1.1/host-linux-x64/python/lib"
and now I get:
$ ./gpukernsum.py report.sqlite
near "WITH": syntax error
... and I'm stuck. The relevant area of the code is:
and not a percentage of the application wall or CPU execution time.
"""
query_stub = """
WITH
summary AS (
SELECT
coalesce({NAME_COL_NAME}, demangledName) AS nameId,
i.e. the "WITH" is part of a string literal which is an SQL query. So, what's the problem? Is Python complaining? Is sqlite complaining?
Note:
Nsight Systems 2022.1.1
CentOS 7
I'm using the original gpukernsum.py code - I have not made any changes to it (other than as described above).
My system has Python 3.9.1 for python3.

A workaround answer:
Nsight Systems bundles its own version of Python, with lib and bin directories.
If you run your script with this specific version, having set PYTHONPATH as described in your question - then the script will work. It's what Nsight itself does, after all.

Related

Python fails to find module when invoked as script ; finds it interactively

I'm trying to run a python script containing submodules from Github to finetune its model.
The final step of the process is to invoke
python realesrgan/train.py -opt options/finetune_realesrgan_x4plus_pairdata.yml --auto_resume
However, doing from root of repo (and from realesrgan folder for that matter) so results in:
Traceback (most recent call last):
File "D:\Real-ESRGAN\realesrgan\train.py", line 5, in
import realesrgan.archs
ModuleNotFoundError: No module named 'realesrgan'
Yet, by launching python interactively from repo root (just "python") and then invoking
import realesrgan.archs
results in me being able to import it without any problem. All mentioned folders have required init.py in them.
This is my first time seeing discrepancy between calling a script and interactive python and I'm wondering, what can cause this.
System specifics just in case:
Windows 11
Python 3.9.9
python script execution fails from either powershell/cmd/file explorer

Impossible to run jupyter-book command after installing it

I'm trying to use Jupyter-book to create automatical PDF from my output, but after installing it using the 'pip install -U jupyter-book' command (which runs successfully), it doesn't recognize jupyter-book when I try to run a command:
Input:
jupyter_book create Jupyter_Book_Name
Output:
SyntaxError: invalid syntax (Temp/ipykernel_16888/3781613275.py, line 1)
File "C:\Users\MAXIME~1.DUS\AppData\Local\Temp/ipykernel_16888/3781613275.py", line 1
jupyter_book create Jupyter_Book_Name
^
SyntaxError: invalid syntax
I already try to add the init jupyter_book function in the sys.path but I get the same error, and the error arises for any command. I am working in Python 3.9.7 with Jupyter Notebook in Visual Studio Code on Windows 10.
Thank you in advance, any help would really help me.
Looks like you're confusing a shell (bash / ...) with a Python REPL.
You're running a shell command in a Python interpreter, a bit like if you try to start Python by typing python inside a Python interpreter:
$ python # Here I'm in bash, I run Python
Python 3.9.7 (default, Sep 3 2021, 06:18:44)
>>> python # Here I'm in the Python interpreter
# typing python again...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'python' is not defined
# Yup, calling `python` here make no sense, Python looks for a variable
# named `python` not a program named `python`, and won't find it.
So get out of a Python interpreter, find a shell (depending on your OS), and run the command again.
Oh while I'm here, it's probably not:
jupyter_book create Jupyter_Book_Name
but:
jupyter-book create Jupyter_Book_Name

Import not working when file is located in same directory and calling script

I'm not sure if the title of this question is correct, mods please feel free to change it.
I'm working through cs231n Convolutional Neural Networks for Visual Recognition course online and I've hit a weird error. This is my first attempt at any real Python programming so it could be a simple error, or something more complicated.
I'm using Python 2.7, Anaconda, and Windows 7.
When trying to run a script I get the following error;
NameError: global name 'im2col_cython' is not defined
My understanding so far is that this occurs because this fails (and calls the exception);
try:
from cs231n.im2col_cython import col2im_cython, im2col_cython
from cs231n.im2col_cython import col2im_6d_cython
except ImportError:
print 'run the following from the cs231n directory and try again:'
print 'python setup.py build_ext --inplace'
print 'You may also need to restart your iPython kernel'
I've tried to figure out why this may be the case. First off I have to run setup.py to turn im2col_cython.pyx into other files. This seems to run but does at one point have the warning;
warning: extension name 'im2col_cython' does not match fully qualified name 'cs231n.im2col_cython' of 'im2col_cython.pyx'
My figuring here is that it's an issue to do with the fact that the folder tree I have looks like this;
Assignment 2
->cs231n
Inside cs231n is where the setup.py and im2col_cython.pyx files are located. I've installed cython, and I have vc for python2.7 installed. When I run setup.py is creates 2 new files;
im2col_cython.c
im2col_cython.pyd
But I have no idea if that's what it's meant to do or anything. I'm in way over my head with this (0 Python knowledge), but I'm keen to learn!
EDIT 1
The script that imports im2col_cython is here
The im2col_cython file is here
EDIT 2
When including the following in the exception clause;
import traceback; traceback.print_exc()
I get this output;
Traceback (most recent call last):
File "fast_layers.py", line 3, in <module>
from cs231n.im2col_cython import col2im_cython, im2col_cython
ImportError: No module named cs231n.im2col_cython
Add this in the file fast_layer.py before import cs231n.im2col_cython:
import pyximport
pyximport.install()
The problem you mentioned should be fixed.

Imports working with raw file, but not in IDLE

UPDATE 10 Secs later
Fixed properly now, and thanks to JF and Gauden.
UPDATE
I have found a temporary fix by saving the IDLE file in the directory the other working file is in. (I would still like to know how to fix it entirely if I can.)
That's not a permanant fix, so if you want to try and help make it work wherever the file is saved, feel free.
This is the start of a python file:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wikipedia
import pagegenerators
import sys
import re
import pywikibot
from pywikibot import *
(You may have noticed it's a pywikipedia script, but I think that's irrelevent)
This file runs fine from the command line.
However, when I try and use IDLE to develop the script, or just use the IDLE interpreter, I get an error:
>>> import wikipedia
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
import wikipedia
ImportError: No module named wikipedia
I don't really have a clue why it isn't working.
I have also tried this:
>>> imp.find_module("wikipedia.py","f:/ake/pa/th/")
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
imp.find_module("wikipedia.py","f:/ake/pa/th/")
ImportError: No frozen submodule named f:/ake/pa/th/.wikipedia.py
The path given in the error log is the correct path to the wikipedia.py file, there's just that . before wikipedia.py.
I then tried adding the path to sys.path, but that didn't work either:
>>> import sys
>>> sys.path.append("c/users/adam/py")
#the same error...
Path to the module: `c:\users\joe_bloggs\py\wikipedia.pyc
Python executable: Command line:C:\Python27\python.exe, IDLE: C:\Python27\pythonw.exe
PYTHONPATH throws, in both:
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
print os.environ['PYTHONPATH'].split(os.pathsep)
File "C:\Python27\lib\os.py", line 423, in __getitem__
return self.data[key.upper()]
KeyError: 'PYTHONPATH'
OS: Windows 7
Python version: 2.7.2
A new PATH: IDLE, and Command Line:
C:\Program Files\Common Files\Microsoft Shared\Windows Live
C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live
C://Python27
C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin
C:\Program Files (x86)\QuickTime\QTSystem\
C:\Program Files (x86)\Windows Live\Shared
EDIT The answer to the above question proved to be fairly simple, but I am editing this answer as a possible troubleshooting checklist for future reference, and as a checklist for others who may need to prepare questions of this nature in the future.
CLUE 1: What is the path to the module you are importing?
>>> import wikipedia
>>> print wikipedia.__file__
This will give you the path to the compiled module, and is one clue.
CLUE 2: What is the path to the Python executable?
(See also this question).
>>> import sys
>>> print sys.executable
Try this in the shell and in an IDLE script. If the two results are different, then you are using two Python interpreters and only one of them has a path that points to the wikipedia module.
CLUE 3: What is the sys.path?
Also repeat this in both shell and as a script in IDLE.
>>> print '\n'.join( sys.path )
(You may be able to use sys.path.append("d:/irectory/folder/is/in") to add that location to the sys.path. This should add that directory to the list of places Python looks for modules.)
CLUE 4: What is the PYTHONPATH and does it differ in the two environments?
(See also this answer).
Finally repeat this in both shell and as a script in IDLE.
>>> import os
>>> print '\n'.join( os.environ['PATH'].split(os.pathsep) )
Again note the two results (from shell and from IDLE) and see if there is difference in the PYTHONPATH in the two environments.
If all these tests prove inconclusive, I would add as much of this information as you can to your question as it would help give you specific further leads. Also add what OS you are using and any tracebacks that you get.
I had the same problem when trying to import a newly installed library on my Raspberry Pi. I followed all the instructions to install the library (Adafruit RHT Sensor) and it worked fine from the terminal. However, I couldn't get it to work from within IDLE.
It turned out that the problem was that the Raspberry Pi has both Python 2 and 3 installed. The install I'd done (using the 'python' command) only applied to Python 2. I had to perform another install using the 'python3' command to install it for Python 3. After that, I restarted IDLE and all worked fine.
The suggestion above to print the sys executable path helped point out the discrepancy:
import sys
print sys.executable

python gtk module opens display on import

I'm using the trick "python -c 'import myscript.py'" to perform a syntax check on a script which uses 'import gtk'.
I get the following error when performing the syntax check, which implies that the gtk module is executing a check for the X display, even though all that's being done at this point is to import the module.
Traceback (most recent call last):
File "<stdin>", line 15, in ?
File "myscript.py", line 21, in ?
import gtk
File "/usr/src/build/463937-i386/install/usr/lib/python2.3/site-packages/gtk-2.0/gtk/__init__.py", line 37, in ?
RuntimeError: could not open display
Is there a way to avoid this error when performing the syntax check?
Before you ask - I'm not able to set $DISPLAY before the syntax check is run. The check is being run on remote servers as part of a distributed build system. These servers do not have an X display available.
Importing modules in Python executes their code!
Well-behaved modules use the if __name__ == '__main__' trick to avoid side effects, but they can still fail - as happened to you.
[BTW, getting to ImportError means the whole file already has correct syntax.]
If you just want to check syntax, without running at all:
python -m py_compile my_script.py
will check one file (and produce a .pyc as a side effect).
python -m compileall ./
will check a whole dir recursively.
python -c 'compile(open("myscript.py").read(), "myscript.py", "exec")'
avoids creating a .pyc.
But note that merely checking the syntax in Python catches very few bugs! Importing does catch more, e.g. mispelled names. For even better checks, use tools like Pychecker / Pyflakes.
What exactly do you mean by 'syntax checking'?
Can't you use a tool like pylint to check for syntax errors?
Otherwise: a very ugly (but probably possible hack):
In your python script detect whether X is present.
If it's not => use GTK on DirectFramebuffer (no X needed then). You'll need to compile GTK on DirectFB (and/or pygtk) from source (some pointers here).
If the remote machine has vncserver installed, you can have a dummy server running and connect to that. Sample instructions:
remotemachine $ vncserver -depth 16 -geometry 800x600 :7
New 'X' desktop is remotemachine:7
Starting applications specified in /home/user/.vnc/xstartup
Log file is /home/user/.vnc/userve:7.log
remotemachine $ DISPLAY=:7 python -c 'import myscript.py'
…
remotemachine $ vncserver -kill :7
Killing Xtightvnc process ID 32058
In your myscript.py, you could do like this
if __name__=="__main__":
import gtk
That will not execute gtk's __init__.py when you do "python -c 'import myscript.py'"
If you are editing with IDLE, Alt+X will check syntax of current file without running it.

Categories