I'm trying to play the video using libvlc with python script, for that i got one script in the stack overflow post.
the script is follows:
import os
import sys
import vlc
if __name__ == '__main__':
#filepath = <either-some-url-or-local-path>
movie = os.path.expanduser(filepath)
if 'http://' not in filepath:
if not os.access(movie, os.R_OK):
print ( 'Error: %s file is not readable' % movie )
sys.exit(1)
instance = vlc.Instance("--sout=#duplicate{dst=file{dst=example.mpg},dst=display}")
try:
media = instance.media_new(movie)
except NameError:
print ('NameError: % (%s vs Libvlc %s)' % (sys.exc_info()[1],
vlc.__version__, vlc.libvlc_get_version()))
sys.exit(1)
player = instance.media_player_new()
player.set_media(media)
player.play()
#dont exit!
while(1):
continue
when i run this code, i'm getting the eror like:
Traceback (most recent call last):
File "test1.py", line 3, in <module>
import vlc
ImportError: No module named vlc
How to import the vlc bindings in to the mechine, can any please help me...
According to Python bindings VLC documentation:
You can download the vlc.py module from the Git repository. It only depends on ctypes (standard module in python >= 2.5). Put the module in some place accessible by python (either next to your application, or in a directory from sys.path).
Related
I want to run a Tor session on a headless computer and I'm using the code from https://github.com/webfp/tor-browser-selenium/blob/master/examples/headless.py
from argparse import ArgumentParser
from tbselenium.tbdriver import TorBrowserDriver
from tbselenium.utils import start_xvfb, stop_xvfb
from os.path import join, dirname, realpath
def headless_visit(tbb_dir):
out_img = join(dirname(realpath(__file__)), "headless_screenshot.png")
# start a virtual display
xvfb_display = start_xvfb()
with TorBrowserDriver(tbb_dir) as driver:
driver.load_url("https://check.torproject.org")
driver.get_screenshot_as_file(out_img)
print("Screenshot is saved as %s" % out_img)
stop_xvfb(xvfb_display)
def main():
desc = "Headless visit and screenshot of check.torproject.org using XVFB"
parser = ArgumentParser(description=desc)
parser.add_argument('tbb_path')
args = parser.parse_args()
headless_visit(args.tbb_path)
if __name__ == '__main__':
main()
But it raises this error:
Traceback (most recent call last):
File "test.py", line 28, in <module>
main()
File "test.py", line 24, in main
headless_visit(args.tbb_path)
File "test.py", line 10, in headless_visit
xvfb_display = start_xvfb()
File "/usr/local/lib/python3.8/dist-packages/tbselenium/utils.py", line 33, in start_xvfb
xvfb_display = Display(visible=0, size=(win_width, win_height))
NameError: name 'Display' is not defined
I'm using:
python 3.8.5
geckodriver v 0.17.0
tor browser v 10.0.5
This is caused by a missing python package 'pyvirtualdisplay'
solution:
pip3 install pyvirtualdisplay
cause:
If you look at the utils.py file referenced in the error you will find that the import of pyvirtualdisplay is nested in a try block with an exception handler that silently passes. The comment suggests it is only needed for tests but that is obviously not true in our case.
I am trying to use python to connect with SF.
Saw some articles that show how to use it with beatbox library and I did install it.
However when trying to run simple code I'm getting error below.
Traceback (most recent call last):
File "c:/Users/user/hello/.vscode/hello.py", line 16, in <module>
import beatbox
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\beatbox\__init__.py", line 1, in <module>
from _beatbox import _tPartnerNS, _tSObjectNS, _tSoapNS, SoapFaultError, SessionTimeoutError
ModuleNotFoundError: No module named '_beatbox'
I navigate to the folder where the beatbox is installed and I see there the file _beatbox.py.
I think the file __init__.py try to import _beatbox but cannot find it for some reason.
Any idea how to solve it? What I'm missing?
Code:
import beatbox
sf_username = "xxxxxx"
sf_password = "xxxxxx"
sf_token = "xxxxxx"
def getAccount():
sf = beatbox._tPartnerNS
sf_client = beatbox.PythonClient()
password = str("%s%s" % (sf_password, sf_token))
sf_client.login(sf_username, sf_password)
accQuery = "Select Id,Name From Account limit 5"
queryResult = sf_client.query(accQuery)
print ("query result: " + str(queryResult[sf.size]))
for rec in queryResult[sf.records:]:
print str(rec[2]) + " : " + str(rec[3])
return
Can close the case. I first found that in python 3+ should use beatbox3. But then found additional errors (possible compatibility issues).
Since I notice it taking me too long, instead I tried using library simple-salesforce 0.74.2 to connect, and it worked perfect.
Probably, Python does not know where to search for the module. By default, only the sitepackages directory and your working directory is searched. You can resove this by placing a symbolic link to the beatbox package or moving it to the sitepackages directory
If you change the sitepackage folder name from "beatbox" to "_beatbox" this will solve your problem. You can then import it as: "import beatbox" and it will load in Python.
contents of io.py
class IO:
def __init__(self):
self.ParsingFile = '../list'
def Parser(self):
f = open(ParsingFile, 'r')
print(f.read())
contents of main.py
import sys
sys.path.insert(0, "lib/")
try:
import io
except Exception:
print("Could not import one or more libraries.")
exit(1)
print("Libraries imported")
_io_ = io.IO()
When I run python3 main.py I get the following error:
Libraries imported
Traceback (most recent call last):
File "main.py", line 11, in <module>
_io_ = io.IO()
AttributeError: module 'io' has no attribute 'IO'
Any idea what's going wrong?
My file was called io. It seems that there already exists a package called io which caused the confusion.
Your package name (io) conflicts with the Python library's package with the same name, so you actually import a system package.
You can check this by printing io.__all__.
Changing io.py to something else is probably the best way to go to avoid similar problems. Otherwise, you can use an absolute path.
try
from io import IO
That worked for me when trying to import classes from another file
this has more information:
Python module import - why are components only available when explicitly imported?
I Am Trying To Play An MP3 File On Python , But I Can't Find The Right Module!
I've Tried This:
import os
os.startfile('hello.mp3')
But I Just Got The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 2, in <module>
os.startfile('hello.mp3')
AttributeError: 'module' object has no attribute 'startfile'
I Have Also Tried This:
import vlc
p = vlc.MediaPlayer("file:hello.mp3")
p.play()
But I Get The Error:
Traceback (most recent call last):
File "/Applications/Youtube/text 2 speech/test.py", line 1, in <module>
import vlc
ImportError: No module named 'vlc'
But I Still Can't Find The Right Module. Could Someone Please Help?
You will need to install the vlc.py module from https://wiki.videolan.org/Python_bindings
The absolute bare bones for this would be something like:
import vlc
Inst = vlc.Instance()
player = Inst.media_player_new()
Media = Inst.media_new_path('/home/rolf/vp1.mp3')
player.set_media(Media)
player.play()
Although you would need to check player.get_state() to see if it is still running, paused, stopped etc and player.stop() to stop the audio once started
As far as I am aware os.startfile() is only available for the windows operating system.
Using the play command line instruction (Linux and probably OS X)
import os
os.system('play /home/rolf/vp1.mp3')
You could also look at Gstreamer, although the documentation could do with some improvement.
import gst
player = gst.element_factory_make("playbin")
player.set_property("uri", "file:///home/james/vp1.mp3")
player.set_state(gst.STATE_PLAYING)
play is stopped with player.set_state(gst.STATE_NULL),
pause with player.set_state(gst.STATE_PAUSED)
Note: Avoid tutorials for Gstreamer version 0.10, search instead for version 1.0
Both vlc and Gstreamer allow you to play audio and video, although, in my opinion, vlc in simpler to use but Gstreamer is more flexible.
import pygame
from pygame import mixer
def play_music():
global paused
if (paused):
mixer.music.unpause()
paused = False
else:
music = choose_music[0]
mixer.music.load(music)
mixer.music.play()
show_details(music)
musicpage.mainloop()
I am following the Curses programming HowTo on the Python site, but I am running into a rather bizarre issue.
My code is currently very short, doesn't actually do anything because of this error, I haven't been able to move on. Here's my code:
import curses
#from curses import wrapper
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
def main(stdscr):
begin_x = 20; begin_y = 7
height = 5; width = 40
win = curses.newwin(height, width, begin_y, begin_x)
stdscr.refresh()
stdscr.getkey()
if __name__ == '__main__':
wrapper(main)
and the Traceback:
Traceback (most recent call last):
File "curses.py", line 1, in <module>
import curses
File "/home/nate/Documents/Programming/Python/curses.py", line 4, in <module>
stdscr = curses.initscr()
AttributeError: 'module' object has no attribute 'initscr'
I commented out the from curses import wrapper because that was giving me another error,
Traceback (most recent call last):
File "curses.py", line 1, in <module>
import curses
File "/home/nate/Documents/Programming/Python/curses.py", line 2, in <module>
from curses import wrapper
ImportError: cannot import name wrapper
but I suppose that would be another question.
I am following the tutorial word for word right now, to learn curses, but currently the only thing it's making me do is use curses directed at Python :P.
I am running Python 3.3.2 on Ubuntu 13.10, so this question has nothing to do with this, as he was using Windows and I am not (thankfully :D)
Why am I not able to do this? I'm copying it directly from the Python site, so you'd think it would work!
You named your file curses.py, so Python thinks that file is the curses module. Name it something else.
mv curses.py someOtherName.py
If you get the same error, try removing any .pyc files.
In this case it would be rm curses.pyc.