Python sdl2 Invalid path - python

I'm testing the learn to fly tutorial for pysdl2. (I'm also new to python)
http://pysdl2.readthedocs.io/en/rel_0_9_4/tutorial/index.html
I'm getting an error, thinking it's just some path problem.
Error:
guillaume#ubuntu:~/script$ python sdlTest.py
Traceback (most recent call last):
File "sdlTest.py", line 4, in <module>
RESOURCES = sdl2.ext.Resources(__file__, "resources")
File "/usr/lib/python2.7/dist-packages/sdl2/ext/resources.py", line 139, in __init__
self.scan(path, subdir, excludepattern)
File "/usr/lib/python2.7/dist-packages/sdl2/ext/resources.py", line 313, in scan
raise ValueError("invalid path '%s'" % path)
ValueError: invalid path 'sdlTest.py'
Current code:
import sys
import sdl2.ext
RESOURCES = sdl2.ext.Resources(__file__, "resources")
I'm on ubuntu 16.04
installed python version
guillaume#ubuntu:~/script$ python -V
Python 2.7.12
python-sdl2 version:
guillaume#ubuntu:~/script$ dpkg -s python-sdl2
Package: python-sdl2
Status: install ok installed
Priority: optional
Section: python
Installed-Size: 392
Maintainer: Ubuntu Developers <ubuntu-devel-discuss#lists.ubuntu.com>
Architecture: all
Source: pysdl2
Version: 0.9.3+dfsg2-1
Depends: python:any (<< 2.8), python:any (>= 2.7.5-5~), libsdl2-2.0-0, libsdl2-gfx-1.0-0, libsdl2-image-2.0-0, libsdl2-mixer-2.0-0, libsdl2-ttf-2.0-0
Recommends: python-numpy
Suggests: pysdl2-doc
Description: Python bindings to the SDL2 C-library (Python 2 build)
PySDL2 is a ctypes based wrapper around the Simple DirectMedia Layer 2 library
to allow portable low level access to a video framebuffer, audio output, mouse
and keyboard.
.
This module is built for Python version 2.x.
Original-Maintainer: Debian Python Modules Team <python-modules-team#lists.alioth.debian.org>
Homepage: https://bitbucket.org/marcusva/py-sdl2
PYTHONPATH:
guillaume#ubuntu:~/script$ echo $PYTHONPATH
/usr/lib/python2.7/dist-packages/sdl2:
guillaume#ubuntu:~/script$ ls /usr/lib/python2.7/dist-packages/sdl2/
audio.py filesystem.py keycode.pyc render.py stdinc.pyc
audio.pyc filesystem.pyc loadso.py render.pyc surface.py
blendmode.py gamecontroller.py loadso.pyc rwops.py surface.pyc
blendmode.pyc gamecontroller.pyc log.py rwops.pyc syswm.py
clipboard.py gesture.py log.pyc scancode.py syswm.pyc
clipboard.pyc gesture.pyc messagebox.py scancode.pyc timer.py
cpuinfo.py haptic.py messagebox.pyc sdlgfx.py timer.pyc
cpuinfo.pyc haptic.pyc mouse.py sdlgfx.pyc touch.py
dll.py hints.py mouse.pyc sdlimage.py touch.pyc
dll.pyc hints.pyc pixels.py sdlimage.pyc version.py
endian.py __init__.py pixels.pyc sdlmixer.py version.pyc
endian.pyc __init__.pyc platform.py sdlmixer.pyc video.py
error.py joystick.py platform.pyc sdlttf.py video.pyc
error.pyc joystick.pyc power.py sdlttf.pyc
events.py keyboard.py power.pyc shape.py
events.pyc keyboard.pyc rect.py shape.pyc
ext keycode.py rect.pyc stdinc.py
guillaume#ubuntu:~/script$

"resource" refers to the name of a sub-directory from where you are running your sdl2-python script.
In the sdl2 example on importing, they need a directory called "resource" to load images that will be displayed them in the sdl2 window in their subsequent examples..
We need some resources from the resources folder, so that we have a
test image around to display on the window later on. In your own
applications, it is unlikely that you will ever need to import them,
but we need them here, so we use the sdl2.ext.Resources class to have
them available.
If you want to continue to use the line:
RESOURCES = sdl2.ext.Resources(__file__, "resources")
then from the directory where you are running your script with this line, create a child-directory called "resources" and run your script. You will see the error message will not appear. You can use names other than "resources", just make sure a child directory with the same name exist.
More importantly, to use sdl2, you just need the following 2 lines to load the sdl2 modules and you are good to go..
import sdl2
import sdl2.ext

You have likely failed to include a resources folder in your project dir.
The error is somewhat ambiguous but this fixed it for me.

Related

Embeddable Python - ModuleNotFoundError for project folders

I'm trying to create an embedded python for my project.
I downloaded the portable python 3.7.6
Copied the folders from my main python installation (Lib, Scripts, include, DLLs etc..)
Added them to the PTH file like so :
python37.zip
.
Lib
Lib/site-packages
Scripts
include
DLLs
doc
libs
Tools
tcl
# Uncomment to run site.main() automatically
#import site
and when I tested it, I tried to run a project with the following structure :
By running the command (using the embedded python):
G:\GitHub\VRoscopy\VRoscopy\Conversion\python\python.exe main.py
I get the following error :
> Traceback (most recent call last):
> File "mymain.py", line 1, in <module>
> import folder.module as m ModuleNotFoundError: No module named 'folder'
However, when I try running the system python everything works normally.
Hope anyone can help me, thanks
The problem was solved by adding the project files to the PTH file, and uncommenting the "import site" like follows :
python37.zip . ../invesalius
../invesalius/* # <----- added this line
Lib
Lib/site-packages
Scripts
include
DLLs
doc
libs
Tools
tcl
# Uncomment to run site.main() automatically
import site

Import from my package recognized by PyCharm but not by command line

I've created the following package tree:
/main_package
/child_package
version.py
where version.py contains a single string variable (VERSION)
Inside my script in child package I'm importing version.py by the following line:
from main_package.version import VERSION
While I'm running the code from PyCharm everything works great, however when I'm running the code via the command line I'm getting the following error message:
C:\Users\usr\PycharmProjects\project\main_package\child_package>python script.py
Traceback (most recent call last):
File "script.py", line 2, in <module>
from main_package.version import VERSION
ModuleNotFoundError: No module named 'main_package'
I've found in the internet that I might need to add my package to the python path, however it doesn't seems to work for me
PyCharm sets the Python Path at the root of the project (by default). To mimic this in a quick'n'dirty fashion, you just need to do this once in your shell session before invoking python whatever:
set PYTHONPATH=C:\Users\usr\PycharmProjects\project
The pythonic way is to have a setup.py file to install your project in the system (check python Minimal Structure):
from setuptools import setup
setup(name='main_package',
version='0.1',
description='main package',
license='MIT',
packages=['main_package'],
zip_safe=False)
Then you install it as follow:
python setup.py install for global installation
OR
python setup.py develop for local installation in editable mode

Sikulix install - jython

I need to test unity 3d app on mobile device (or emulator). I do not want to press on buttons using coordinates only, so I want to use Sikuli. I will describe steps that I have already done:
Downloaded and installed Sikuli v1.1.1
Installed java 32bit
Installed jython 2.7.0
jython added as interpreter in my project in pyCharm
sikulixapi is run
Copied Sikuli lib from appData to jython folder
Created Python file and added imports
And when I try to run project I receive this error:
D:\jython2.7.0\bin\jython.exe -Dpython.path=D:\python_projects\sikuli_test D:/python_projects/sikuli_test/test_sikuli.py
Tra**ceback (most recent call last):
File "D:/python_projects/sikuli_test/test_sikuli.py", line 1, in <module>
from sikuli import Region
File "D:\jython2.7.0\Lib\sikuli\__init__.py", line 5, in <module>
from org.sikuli.basics import Debug
ImportError: No module named sikuli
Process finished with exit code -1**
What am I doing wrong?
The following works for me:
import org.sikuli.script.SikulixForJython
before
from sikuli import *
In your jython project interpreter, set the environment variables path to where your sikuli jar is placed. That should fix it.
Also make sure to use this in your code
import org.sikuli.script.SikulixForJython
from sikuli import *

Debianzing a Python program to get a .deb [duplicate]

This question already has answers here:
Is there a standard way to create Debian packages for distributing Python programs?
(5 answers)
Closed 5 years ago.
Aim
To create an installable .deb file (or package). Which when clicked would install the software on a Linux machine and an icon would be put on the GNOME panel. So as to launch this application from there.
What I have referred to
I referred to two debianizing guides.
Guide 1
Guide 2
The first one had a video which was impossible to understand, partly because of the accent and partly because it was hopelessly outdated.(it was uploaded in 2007)
And the second one was completely text. I got till the 4th Step, Builds the package. But when I did it I got output that did not match what was given in the guide.
What I require
I have a simple python program. It takes your age and then prints back out if the age is below, equal to, or above 18 years. There is just one file and no other dependency for this program. And I want to build this into a .deb.
Specs
-Python 2.7
-Linux Mint
Edit
I followed the exact directory structure as you instructed as you. And replaced all myscript with cowsandbulls. The build completed and I got the Debian. When I installed it and then ran the command cowsandbulls from the terminal I got the following error:
Traceback (most recent call last):
File "/usr/bin/cowsandbulls", line 9, in <module>
load_entry_point('cowsandbulls==1.0', 'gui_scripts', 'cowsandbulls')()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 337, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2311, in load_entry_point
return ep.load()
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2017, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named cowsandbulls
I just tested stdeb (see https://pypi.python.org/pypi/stdeb), a Python package for turning any other Python package into a Debian package.
First I installed stdeb:
apt-get install python-stdeb
Then I made a simple script called myscript.py with the following contents:
def main():
print "Hello world, says myscript!"
# wait for input from the user
raw_input()
if __name__ == '__main__':
main()
Importantly, your directory structure should be:
somewhere/myscript/
setup.py
myscript/
__init__.py
myscript.py
In the setup.py file, you do something like:
import os
from setuptools import setup
from nvpy import nvpy
setup(
name = "myscript",
version = "1.0",
author = "Charl P. Botha",
author_email = "cpbotha#vxlabs.com",
description = "Demo of packaging a Python script as DEB",
license = "BSD",
url = "https://github.com/cpbotha/nvpy",
packages=['myscript'],
entry_points = {
'console_scripts' : ['myscript = myscript.myscript:main']
},
data_files = [
('share/applications/', ['vxlabs-myscript.desktop'])
],
classifiers=[
"License :: OSI Approved :: BSD License",
],
)
The console_scripts directive is important, and it'll create an executable script called my_script, which will be available system-wide after you install the resultant DEB. If your script uses something like tkinter or wxpython and has a graphical user interface, you should use gui_scripts instead of console_scripts.
The data_files directive will install a suitable desktop file into /usr/share/applications, so that you can also start myscript from your desktop environment. vxlabs-myscript.desktop looks like this:
[Desktop Entry]
Version=1.0
Type=Application
Name=myscript
Comment=Minimal stdeb example
# myscript should wait for user input at the end, else the terminal
# window will disappear immediately.
Exec=myscript
Icon=/usr/share/icons/gnome/48x48/apps/file-manager.png
Categories=Utility;
# desktop should run this in a terminal application
Terminal=true
StartupNotify=true
StartupWMClass=myscript
To build the DEB, you do the following in the top-level myscript:
python setup.py --command-packages=stdeb.command bdist_deb
Which will create a .deb in the deb_dist directory.
After having installed the DEB I created like this, I could run myscript from the command-line, and I could also invoke it from my desktop environment.
Here's a GitHub repository with the example code above: https://github.com/cpbotha/stdeb-minimal-example
The right way of building a deb package is using dpkg-buildpackage, but sometimes it is a little bit complicated. Instead you can use dpkg -b <folder> and it will create your Debian package.
These are the basics for creating a Debian package with dpkg -b <folder> with any binary or with any kind of script that runs automatically without needing manual compilation (Python, Bash, Pearl, and Ruby):
Create the files and folders in order to recreate the following structure:
ProgramName-Version/
ProgramName-Version/DEBIAN
ProgramName-Version/DEBIAN/control
ProgramName-Version/usr/
ProgramName-Version/usr/bin/
ProgramName-Version/usr/bin/your_script
The scripts placed at /usr/bin/ are directly called from the terminal. Note that I didn't add an extension to the script.
Also, you can notice that the structure of the deb package will be the structure of the program once it's installed. So if you follow this logic if your program has a single file, you can directly place it under ProgramName-Version/usr/bin/your_script, but if you have multiple files, you should place them under ProgramName-Version/usr/share/ProgramName/all your files and place only one file under /usr/bin/ that will call your scripts from /usr/share/ProgramName/.
Change all the folder permission to root:
chown root:root -R /path/to/ProgramName-Version
Change the script's permissions:
chmod 0755 /path/to/the/script
Finally, you can run: dpkg -b /path/to/the/ProgramName-Version and your deb package will be created! (You can also add the post/pre inst scripts and everything you want; it works like a normal Debian package.)
Here is an example of the control file. You only need to copy-paste it in to an empty file called "control" and put it in the DEBIAN folder.
Package: ProgramName
Version: VERSION
Architecture: all
Maintainer: YOUR NAME <EMAIL>
Depends: python2.7, etc , etc,
Installed-Size: in_kb
Homepage: http://foo.com
Description: Here you can put a one line description. This is the short Description.
Here you put the long description, indented by 1 space.
If you want to build using dpkg -b <folder> you can use this program that will do everything with one command. If you regularly build packages, it is a pain to do all the stuff that I mentioned!
*The guide was taken from Basics of Debian Packages.

PIL - libjpeg.so.8: cannot open shared object file: No such file or directory

Compiled the libjpeg v8, PIL 1.1.7 and and import for _imaging works on the system Python, but spouts this error inside the virtualenv:
libjpeg.so.8: cannot open shared object file: No such file or directory
here is the error run with a python -v interpreter inside the virtualenv
>>> import _imaging
dlopen("/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/PIL/_imaging.so", 2);
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libjpeg.so.8: cannot open shared object file: No such file or directory
and here are the paths:
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/distribute-0.6.14-py2.6.egg
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/pip-0.8.1-py2.6.egg
/home/ygamretuta/dev/py/django/lib/python2.6
/home/ygamretuta/dev/py/django/lib/python2.6/plat-linux2
/home/ygamretuta/dev/py/django/lib/python2.6/lib-tk
/home/ygamretuta/dev/py/django/lib/python2.6/lib-old
/home/ygamretuta/dev/py/django/lib/python2.6/lib-dynload
/usr/lib/python2.6
/usr/lib/python2.6/plat-linux2
/usr/lib/python2.6/lib-tk
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages
/home/ygamretuta/dev/py/django/lib/python2.6/site-packages/PIL
I am using Ubuntu 10.10 and this is the uname-a output:
Linux ygam-desktop 2.6.35-28-generic #49-Ubuntu SMP Tue Mar 1 14:40:58 UTC 2011 i686 GNU/Linux
I am using Python 2.6
I followed the following guides already:
http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/
http://www.jooncode.com/2010/12/02/python-pil-jpeg-resync-restart-error-imaging-module-solve/
http://djangodays.com/2008/09/03/django-imagefield-validation-error-caused-by-incorrect-pil-installation-on-mac/
See an explanation here: Why can't Python find shared objects that are in directories in sys.path?
A quick fix is to add the directory that contains libjpeg.so.8 to your /etc/ld.so.conf file, and then run ldconfig
Also if you are doing local Python installations you can also control dynamic linking on the session level using LD_LIBRARY_PATH environment variable::
export LD_LIBRARY_PATH=/srv/plone/python/python-2.6/lib
python
import _imaging
...
This way you cannot break your OS itself, even accidentally. (It happens: http://opensourcehacker.com/2011/08/31/zend-server-installation-potentially-kills-your-ssh/)
Compiling the library from source works too: http://www.ijg.org/files/
Make sure to use jpegsrc.v8.tar.gz if you're on a Unix like system though. jpegsr8.zip appears to be the MS version, and throws all the standard formatting issues while building.
maybe just install libjpeg
conda install -c conda-forge libjpeg-turbo

Categories