I'm trying to import selenium my first time into my new Python project. But when I run the program it does not work and shows the following message below.
I checked if python and selenium is installed correctly. I even tried to reinstall it but still, the same message shows up. I might have made a stupid mistake somewhere as I'm just starting off with Python and Selenium. Does anyone have a solution?
My code:
from selenium import webdriver
from selenium.webdriver.comma.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
This is the output:
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\Henryk\Documents\Selenium sample
project\SeleniumSampleProject.py
Traceback (most recent call last):
File "C:\Users\Henryk\Documents\Selenium sample
project\SeleniumSampleProject.py", line 2, in <module>
from selenium.webdriver.comma.keys import Keys
ModuleNotFoundError: No module named 'selenium.webdriver.comma'
>>>
Try to common:
from selenium.webdriver.common.keys import Keys
Related
I am running the python interpreter from the same directory where the imported file my_module.py resides. When I try to use the function defined in the imported file using only the function name print_hi, I get a NameError error message. But it works fine if I reference the function using my_module.print_hi
Is there a way to make this work without referencing the module file name?
Is there a way to make this work without using the from keyword in the import statement?
PS C:\Users\joe\Documents\Python\General\importing> type my_module.py
def print_hi(name):
print(f'Hi, {name}')
PS C:\Users\joe\Documents\Python\General\importing> python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import my_module
>>> print_hi('joe')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'print_hi' is not defined
>>> my_module.print_hi('joe')
Hi, joe
>>>
If you want to refer to your print_hi function without using the module name, you must import it explicitly or import everything from the module:
from my_module import print_hi
# or import everything:
from my_module import *
You can use from keyword
from my_module import print_hi
print_hi('Joe')
From is used to import only a specified section from a module.
I am attempting to create a python script that connects to an SQLITE database and using SQLAlchemy to help with this.
I am still very early, but am trying to create a connection to a new database but keep getting this "create_engine" is not defined in SQLAlchemy error.
To try to simplify I opened a python terminal to try it... see below:
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> engine = create_engine('sqlite:///test.db')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'create_engine' is not defined
>>> print(sqlalchemy.__version__)
1.3.18
>>>
At this point I don't even know where to go looking for the problem.
I did a pip uninstall sqlalchemy then pip install sqlalchemy hoping this might help.
Thanks to Gord Thompson. Changing this line:
engine = create_engine('sqlite:///test.db')
to:
engine = db.create_engine('sqlite:///test.db')
made it work!!
I am trying to load sqlite 64 bit while running Python 2.7 64 bit. I can do this interactively, but, not from a script.
Interactive:
$ /c/Python27-64/python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>>
From this script, one single line, the same as was done from the python shell:
import sqlite3
Run from command line:
$ /c/Python27-64/python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
import sqlite3
File "c:\Python27-64\lib\sqlite3\__init__.py", line 24, in <module>
from dbapi2 import *
File "c:\Python27-64\lib\sqlite3\dbapi2.py", line 28, in <module>
from _sqlite3 import *
ImportError: DLL load failed: %1 is not a valid Win32 application.
The script is obviously finding a 32 bit dll. But why? What is the difference between interactive and from the single line script? How is the DLL search being modified?
In case anyone runs into this, the problem was the file _sqlite3.pyd in the directory I was running the script. Can someone explain why Python creates it's own version of the Windows dll? Is this simply wrapped so that Python can make calls into it? Perhaps a ctypes wrapper?
I'm trying to use generateDS under windows, which uses os.tmpfile. Unfortunately, os.tmpfile doesn't work for me:
(oneclickcos) C:\Users\Marcin\Documents\oneclickcos\xsd>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.tmpfile()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 13] Permission denied
>>>
I've got all my temp directories set with full control for everyone, so that shouldn't be the problem.
What could be causing this?
Run the script as administrator (right click on the script and select 'run as administrator'), the script lacks the permissions to execute os.tmpfile().
Edit:
As I see you're using the interpreter, simply run the interpreter as administrator. If you're accessing it though a terminal, running the terminal as administrator should be sufficient.
As Griffin pointed out the problem is that the os.tmpfile() tries to create a file in the root directory. If you don't like to run the script as administrator you can use os.tmpnam() and handle the file yourself.
Warning: Use of tmpnam() is vulnerable to symlink attacks
I have an error when i used pyqrcode.
[root#localhost python2.6]# python
Python 2.6.5 (r265:79063, Sep 7 2010, 07:31:57)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import qrcode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/site-packages/qrcode-0.2.1-py2.6-linux-x86_64.egg/qrcode/__init__.py", line 6, in <module>
from qrcode import _qrcode
ImportError: cannot import name _qrcode
How to resolve above error?
I am referring pyqrcode from http://pyqrcode.sourceforge.net/
Thanks,
Manu
After the installation of PIL-1.1.7 and JCC-2.14, I've tried to install pyqrcode-0.2.1 from sources as you did, and also ran into the same error :
ImportError: No module named _qrcode. But then I've noticed that _qrcode is actually a lib (_qrcode.so). So I've tried to add it on my library path :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/python2.7/site-packages/qrcode-0.2.1-py2.7-linux-x86_64.egg/qrcode/
And it worked ! Well actually, not quite, I ran into another error :
AttributeError: 'module' object has no attribute '_setExceptionTypes'
So I've edited the __init__.py file
# probably located under a path like this for linux
/usr/local/lib/python2.7/site-packages/qrcode-0.2.1-py2.7-linux-x86_64.egg/qrcode/
# or under a path like this for a Mac
/Library/Python/2.7/site-packages/qrcode-0.2.1-py2.7-macosx-10.7-intel.egg/qrcode/
and commented out line 21 :
# _qrcode._setExceptionTypes(JavaError, InvalidArgsError)
Then I was able to run their simple example :
#!/usr/bin/env python
# coding: utf-8
#
# pyqrcode sample encoder
import sys, qrcode
e = qrcode.Encoder()
image = e.encode('woah!', version=15, mode=e.mode.BINARY, eclevel=e.eclevel.H)
image.save('out.png')
(source : http://pyqrcode.sourceforge.net/)
Hope it helps,