I have a py file that preforms a google search using the system arguments passed to it and opens the first five results of the search as separate browser tabs (this is an exercise from the book Automate the Boring Stuff).
I would like to execute this script via the Windows run command and have therefore created a BAT file.
Currently, when I execute the BAT file, a "module not found" error is returned.
I suspected this issue was related to the fact that the modules required by the py file were only installed in the virtual environment of my specific python project (I have a project specifically for the exercises in this book). Therefore, I installed the necessary modules directly into the environment of my main Python installation. Unfortunately, this had no effect.
I then revised my BAT file by adding a line to activate my virtual environment before the line to execute my py file. This seemed to prevent my py file from being executing by the BAT file.
I'm somewhat familiar with Python but new to BAT files and the command line in general. I've read through a basic command line tutorial but couldn't find anything that helps.
I'm not sure how to resolve this issue and if possible would like to avoid polluting my main python environment with tons of modules. Is this possible and, if so, what am I missing?
.bat file
@py.exe C:\Users\Betty\PycharmProjects\Automate_the_Boring_Stuff\12\searchpypi.py %*
@pause
.py file
#! python3
# searchpypi.py - Opens several search results.
import sys
import webbrowser
import requests
import bs4
print('Searching...') # display text while downloading the search result page
res = requests.get('https://google.com/search?q=' 'https://pypi.org/search/?q='
+ ' '.join(sys.argv[1:]))
res.raise_for_status()
# Retrieve top search result links.
soup = bs4.BeautifulSoup(res.text, 'html.parser')
# Open a browser tab for each result.
linkElems = soup.select('.package-snippet')
numOpen = min(5, len(linkElems))
# Open a browser tab for each result.
for i in range(numOpen):
urlToOpen = 'https://pypi.org' + linkElems[i].get('href')
print('Opening', urlToOpen)
webbrowser.open(urlToOpen)
edit (Fri Mar 05 16:41:27 2021 UTC):
Error message (copied from command line):
Traceback (most recent call last):
File "C:\Users\Betty\PycharmProjects\Automate_the_Boring_Stuff\12\searchpypi.py", line 8, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Press any key to continue . . .
edit (Sat Mar 06 07:39:43 2021 UTC):
Pip Details:
C:\Users\Betty>pip -V
pip 21.0.1 from c:\python38\lib\site-packages\pip (python 3.8)
C:\Users\Betty>pip3 list
Package Version
---------------- ----------
appdirs 1.4.4
certifi 2020.12.5
chardet 4.0.0
distlib 0.3.1
filelock 3.0.12
idna 2.10
pip 21.0.1
pipenv 2020.11.15
requests 2.25.1
setuptools 41.2.0
six 1.15.0
urllib3 1.26.3
virtualenv 20.2.2
virtualenv-clone 0.5.4
wheel 0.34.2
C:\Users\Betty>