Python tbselenium Display is not defined - python

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.

Related

how to import module kicost and pass arguments

I'm trying to import the module kicost in the python script and call the main function with arguments.
So far, I've been unsuccessful to do it after many trials.
The module has been installed with pip.
import sys, os
from kicost import *
import importlib
spam_spec = importlib.util.find_spec("kicost")
print(spam_spec)
# sys.argv.append('-h')
main()
exit()
Here is the execution log:
ModuleSpec(name='kicost',
loader=<_frozen_importlib_external.SourceFileLoader object at
0x101278160>,
origin='/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost/init.py',
submodule_search_locations=['/Users/sebo/Projects/python/pandas/env/lib/python3.8/site-packages/kicost'])
Traceback (most recent call last): File "test-import-kicost.py",
line 12, in
main() NameError: name 'main' is not defined
I guess there is something I don't understand with the import.
Could somebody help me? Thanks.
S/
Finally, I managed to do it:
import sys, os
import kicost.__main__ as kicost
sys.argv.append('-i=/Users/sebo/Projects/python/pandas/test.csv')
sys.argv.append('--currency=EUR')
sys.argv.append('--overwrite')
kicost.main()
exit()

Python import from script outside package

Python unable to import package, but works correctly from within the package. A fully functional example below. In the virtual env I am using 3.6 All responses greatly appreciated!
parsers/
__init__.py
element.py
parser1.py
parser2.py
parserresolver.py
outsidepkg.py
init.py is empty
element.py:
def router():
pass
parser1.py:
from element import *
def parse(data):
return data
parser2.py:
from element import *
def parse(data):
return data
parserresolver.py:
import sys
from parser1 import *
from parser2 import *
def resolve(data):
parseddata = None
parsers = ['parser1', 'parser2']
funcname = 'parse'
for parser in parsers:
module = sys.modules[parser]
if hasattr(module, funcname):
func = getattr(module, funcname)
parseddata = func(data)
print(parseddata)
return parseddata
if __name__ == "__main__":
resolve('something')
outsidepkg.py:
import parsers.parserresolver
def getapi(data):
parsers.parserresolver.resolve(data)
if __name__ == "__main__":
print(getapi('in parse api main'))
So when I call parserresolver.py directly it works great, no import errors and prints out "something" as expected.
But when I call outsidepkg.py I am getting this error:
Traceback (most recent call last):
File "C:\code\TestImport\TestImport\outsidepkg.py", line 1, in <module>
import parsers.parserresolver
File "C:\code\TestImport\TestImport\parsers\parserresolver.py", line 2, in <module>
from parser1 import *
ModuleNotFoundError: No module named 'parser1'
Press any key to continue . . .
You need to change the imports of:
from file import whatever
To:
from .file import whatever
Since your code to run it is outside the folder, use a . to get the directory, since the file isn't outside the package.

how to import vlc plugins in to python script

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).

Curses returning AttributeError: 'module' object has no attribute 'initscr'

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.

error when using import command in python

I'm new to python and now learning how to to import a module or a function, but I got these posted errors. The python code is saved under the name: hello_module.py
python code:
def hello_func():
print ("Hello, World!")
hello_func()
import hello_module
hello_module.hello_func()
error message:
Traceback (most recent call last):
File "C:/Python33/hello_module.py", line 9, in <module>
import hello_module
File "C:/Python33\hello_module.py", line 10, in <module>
hello_module.hello_func()
AttributeError: 'module' object has no attribute 'hello_func'
You cannot and should not import your own module. You defined hello_func in the current namespace, just use that directly.
You can put the function in a separate file, then import that:
File foo.py:
def def hello_func():
print ("Hello, World!")
File bar.py:
import foo
foo.hello_func()
and run bar.py as a script.
If you try to import your own module, it'll import itself again, and when you do that you import an incomplete module. It won't have it's attributes set yet, so hello_module.hello_func doesn't yet exist, and that breaks.

Categories

Resources