Executing python script with selenium with batch (selenium error) - python

I'm trying to execute a python script with selenium module via batch file.
The python script itself runs perfectly OK, but when I try to execute the script through a .bat file it gives me the error 'ModuleNotFoundError: No module named 'selenium''
from selenium import webdriver
driver = webdriver.Chrome(executable_path='C:/Temp/chromedriver.exe')
driver.get('http://www.example.com')
C:\Python\Python37\python.exe C:\PythonTest\testFile.py
The error printed is:
Traceback (most recent call last):
File "C:\Users\ElGregory\PycharmProjects\PythonTest\testFile.py", line 1, in <module>
from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
Which obviously is a Python error, but when the code is run in Pycharm it runs as expected. (=selenium installed correctly)
Any help apreciated.

This could be because when you are running inside Pycharm, the libraries are installed in your virtual python environment (venv).
Either activate the virtual environment before running the python file for which you can read more at https://docs.python.org/3/library/venv.html
Or install your libraries globally

Related

Pycharm doesn't recognise Sqoop libraries

I am on Pycharm trying to use Sqoop import job to load MySQL data in to HDFS.
I downloaded this package on terminal
pip install pysqoop
I tried running this package
from pysqoop.SqoopImport import Sqoop
sqoop = Sqoop(help=True)
code = sqoop.perform_import()
This was the error
/home/amel/PycharmProjects/pythonProject/venv/bin/python /home/amel/PycharmProjects/pythonProject/Hello.py
Traceback (most recent call last): File "/home/amel/PycharmProjects/pythonProject/Hello.py", line 1, in <module>
from pysqoop.SqoopImport import Sqoop ModuleNotFoundError: No module named 'pysqoop'
Process finished with exit code 1
How can I solve this problem?
There is an option that your python code is running in a different python environment than your main one go to pycharm file->settings->project->project-interpreter. Than change your env.
OR
put your cursor on pysqoop and press alt+enter and choose "install package pysqoop"

When trying to import selenium it does not work

When i attempt to import selenium i get the error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import selenium
ModuleNotFoundError: No module named 'selenium'
My selenium module is currently in:
C:\Users\Maseeek\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium
I've seen other people have it in a different directory and want to know how to fix it.
If you are using "PYCHARM" and if you created project with "Virtual Environment" you should choose "Python 3.x.x" instead of "Virtual Environment Python 3.9" that PyCharm created.
If you installed "anaconda" before you need to change paths of libaries.
If you're using PyCharm, then this might help:
PyCharm sometimes shows an error in importing modules that have already been installed (that's annoying). But, try to change the file location to this destination:
C:\Users\Maseeek\PyCharm Projects\(project name)\venv\site-packages\
or try installing the package in PyCharm using python with the following code:
from sys import *
from subprocess import *
call(["-m", "pip", "install", "selenium"])

I can`t import wxpython in anywhere except python console

I installed python 3.8.8 and installed wxpython using pip at terminal
pip install wxpython
and i run simple program
import wx
print(wx.version())
in pycharm and pycharm`s python console, I got
ModuleNotFoundError: No module named 'wx'
in IDLE, I got
Traceback (most recent call last):
File "C:/Users/tasoo/OneDrive/Desktop/wx.py", line 1, in <module>
import wx
File "C:/Users/tasoo/OneDrive/Desktop\wx.py", line 2, in <module>
print(wx.version())
AttributeError: partially initialized module 'wx' has no attribute 'version' (most likely due to a circular import)
in python.exe code works
I want to import wx in pycharm project.
I tried add python in system path but it didn`t work.
You have problem because you saved code in file wx.py and now import wx loads your file wx.py instead of module wx. Rename your file - ie. main.py instead of wx.py
PyCharm may have own Python installed and it may need to install wx in this Python.
Check
import sys
print(sys.executable)
to get full path to Python used by PyCharm and then use this path
/full/path/to/python -m pip install wx
Or search in PyCharm settings (in menu File) and change Python Interpreter.
In PyCharm for every project you may set different Python - if you have installed many versions.

Running python file from command prompt ModuleNotFoundError: No module named 'pygame'

I have this error when running the script from terminal but works from PyCharm
C:\Users\Username\PycharmProjects\Space Invaders>python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
This is how my file directory looks:
https://i.stack.imgur.com/s9qB5.png
I am using python 3.8 and pygame 2.0.1
Should I have to install pygame globally for me to run the script from command line? I have the package installed in a virtual environment.
You're trying to execute the script with global python which doesn't have the pygame package installed. So, you have to activate the virtual environment first. To do this, go to venv/Scripts/ and there will be an "activate" file that you need to execute. Once you have done this you can run your script and it should work.
More info on: https://docs.python.org/3/tutorial/venv.html

CentOS Python multiple interpreters issue - incorrect shebang

I'm trying to run a Python script (testing.py) as a cron job. I have CentOS with a separate python2.7 interpreter path, so I've added the following shebang:
#!/usr/local/bin/python2.7
However, when the cron job runs I get the following error
Traceback (most recent call last):
File "/root/carsales/carsales_v2.py", line 3, in <module>
import pandas as pd
ImportError: No module named pandas
I have installed Anaconda, and if I run the python2.7 interpreter through the command line, as:
python2.7 testing.py
It imports pandas successfully. However, running it directly (as per below) fails to import:
./testing.py
So there's some problem with the shebang, or possibly with the Anaconda install?

Categories

Resources