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.
Related
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.
I am new in python and pyqt5. I wrote a simple project by python and pyqt5 and now i want to close source code to not seen my source code. I think by obfuscate i can do this so i have installed pyminifier.
sudo apt install python-setuptools
pip3 install pyminifier
In my project i have a few class. By this command i can obfuscate one class of my project:
pyminifier --obfuscate --gzip MainWindow.py
import zlib, base64
exec(zlib.decompress(base64.b64decode('eJytVU1v2zAMvftXaLnUGQJtl10G+NAmHbAObfPRIAWGwVBtylZrS64k56PYj59oO46dpNtlh8QW+UQ9PlI01yonS3HLhF.....
The above output is shown in my terminal after running pyminifier --obfuscate.
Does it commend must be run for each class and copy output code to other new file?
If i do, I think these class's that imported together properly do not known each other and finally application do not be run!!! I am right?
Edit
I obfuscate entrypoint of my project by this command:
pyminifier --obfuscate --gzip Main.py
import zlib, base64
exec(zlib.decompress(base64.b64decode('eJzLzC3ILypRKK4s5sqyBZJ6qRWZJVzuYGZiUXoZV1pRfq5CQGVgialeYEl4Zkp6akmxQiZEV6BjQUFOZnJiSWZ+HkShb2JmXnhmXkp+OUwNQgSomMvHFlmPhrsmV6UtigoNTa4sDR+gK1KTNTQ1uQAalDgb')))
# Created by pyminifier (https://github.com/liftoff/pyminifier)
and i copied this codes into other file.py and i run app but i got Error:
python new.py
Traceback (most recent call last):
File "new.py", line 2, in <module>
exec(zlib.decompress(base64.b64decode('eJzLzC3ILypRKK4s5sqyBZJ6qRWZJVzuYGZiUXoZV1pRfq5CQGVgialeYEl4Zkp6akmxQiZEV6BjQUFOZnJiSWZ+HkShb2JmXnhmXkp+OUwNQgSomMvHFlmPhrsmV6UtigoNTa4sDR+gK1KTNTQ1uQAalDgb')))
File "<string>", line 8
j(L.exec())
^
SyntaxError: invalid syntax
Are you running a Python 3 program with Python 2? The error message only makes sense if you are using Python 2, where exec was a keyword.
For obfuscating multiple files, see http://liftoff.github.io/pyminifier/pyminifier.html:
Pyminifier can now minify/obfuscate an arbitrary number of Python scripts in one go. For example, ./pyminifier.py -O *.py will minify and obfuscate all files in the current directory ending in .py. To prevent issues with using differentiated obfuscated identifiers across multiple files, pyminifier will keep track of what replaces what via a lookup table to ensure foo_module.whatever is gets the same replacement across all source files. Added in version 2.0
OS: Fedora 21
Python: 2.7.6
I run a python script as root or using sudo it runs fine. If I run it as just the user I get the following:
Traceback (most recent call last):
File "/home/user/dev_ad_list.py", line 12, in
import ldap
ImportError: No module named ldap
selinux=disabled -- What other security is preventing a user from running a python script that imports ldap
If it works fine under sudo, it simply sounds like a file access issue.
A quick fix for this would be to run something along the lines of:
sudo chmod -R a+rX /usr/lib/python2.7
But you may wish to be more specific with the directory (or even file) that you actually apply this to.
Path to python was different than other user. User was pointing to canopy.
I downloaded a program called "A ren'py script decompiler," and I cannot for the life of me work out how to run it.
I googled a bit to discover that the files I had downloaded, which were in ".py" format, referred to Python, (yes, this is the level I am on,) so I downloaded it, and after a bit more googling, managed to run the script.
However I have still no idea how to get this thing to actually do what I want, as when I try to copy the commands from the README, it just brings up errors, and half the time I don't even understand what the README is saying in the first place. For example: "You can either use the -b argument to specify the directory in which renpy lies or place the renpy module in your Python module search path." This is gibberish to me.
So I know this is a big ask, but apparently getting this to run is supposed to be pretty simple -- it's just a lightweight program/script/thing that decompiles .rpyc scripts. So does anyone know how I can get it to work?
This is the thing: https://github.com/yuriks/unrpyc
When I run the script in Python it just says:
usage: unrpyc.py [-h] [-c] [-b BASEDIR] [-d]
[--python-screens | --ast-screens | --no-screens | --single-line-screen-kwargs]
file [file ...]
unrpyc.py: error: too few arguments
And then after that I don't know what to type to get it to work anyways.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
--basedir
NameError: name 'basedir' is not defined
I'm the maintainer of unpryc
The script should be ran with python 2.7. Also I'm glad to tell you that in the more recent version of the decompiler (http://github.com/CensoredUsername/unrpyc) the dependency on renpy has been removed, so you no longer have to fiddle with the --basedir option.
regarding the error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
--basedir
NameError: name 'basedir' is not defined
I'd guess that's caused because you're trying to run the command in a python shell instead of a normal command prompt. If you want to call the script normally open a command prompt (I think you're on windows, so it'd be cmd.exe) and run the command:
path_to_python_executable unrpyc.py script_you_want_to_decompile.rpyc
path_to_python_executable is usually C:\Python27\python.exe
That should work.
You won't need most options; the only one you probably need is the -b switch, and of course the script you want to decompile.
The -b (or --basedir) option tells this script where to find the Ren'Py source code; you need to have that program installed too, and then give the directory where it's Python modules are located to this script. Presumably just downloading the SDK, extracting that somewhere and naming that path with teh -b switch is enough:
unrpyc.py -b /path/to/renpy/modules/directory script_you_want_to_decompile.rpy
I'm trying to set GST_PLUGIN_PATH environment variable before loading custom gstreamer plugin in an integration test, so I need to change GST_PLUGIN_PATH programmatically.
But without GST_PLUGIN_PATH set in shell, gst.element_factory_make fails.
I'm using gstreamer-0.10, python2.6 and linux 3.2.0 (debian 6).
Example:
import gst, os
os.environ['GST_PLUGIN_PATH'] = '/tmp'
print gst.element_factory_make('myelem')
Without GST_PLUGIN_PATH set in shell:
$ export GST_PLUGIN_PATH=
$ ./gstpathtest.py
Traceback (most recent call last):
File "./gstpathtest.py", line 7, in <module>
print gst.element_factory_make('myelem')
gst.ElementNotFoundError: myelem
With GST_PLUGIN_PATH set in shell:
$ GST_PLUGIN_PATH=/tmp ./gstpathtest.py
/MyElem:myelem0 (__main__.MyElem)
or with GST_PLUGIN_PATH exported in shell:
$ export GST_PLUGIN_PATH=/tmp
$ ./gstpathtest.py
/MyElem:myelem0 (__main__.MyElem)
When run with GST_DEBUG=6 I noticed that myelem gets created but is immediately unref-ed and destroyed, which is probably the cause of the error.
I even tried to add the path to os.environ['PATH'], or directly to sys.path but it didn't change anything.
My main question is: am I doing something wrong on python-level (and what exactly) or does it indicate some bug in the myelem plugin itself?
Oh, stupid me - if I set os.environ['GST_PLUGIN_PATH'] before importing gst, everything works as expected.
Also, gst.registry.scan_path works:
import gst
gst.registry_get_default().scan_path('/tmp')
print gst.element_factory_make('myelem')