Python terminal closes when importing BeautifulSoup - python

I have a simple python program, that is supposed to scrape some information from the internet and do stuff with it. When I run the code in PyCharm (IDE) it works fine, but when I run it directly it doesn't work (Right-click -> Open with -> Python). In order to find the error I put several input statements in the code, as such:
from random import choice
input(1)
import webbrowser as web
input(2)
from time import sleep
input(3)
import pyautogui as pg
input(4)
from platform import system
input(5) # It closes after this.
from bs4 import BeautifulSoup
input(6)
import requests
input(7)
It all seems to work fine until 5. I press enter then the terminal instantly closes without any error messages. So there must be something wrong when I import BeautifulSoup from bs4.
from bs4 import BeautifulSoup
Why does the python terminal close when executing the import statement? Any help would be appreciated :)

This is probably due to an exception that is terminating the process. I had the same problem a while ago and it was solved by installing the package.
Are you sure you have bs4 installed? Try running pip install bs4 to make sure it is installed.

Related

org.openqa.selenium.JavascriptExecutor could be resolved

I was trying to insert a js code into a website using selenium.
I know that I want to use the following import statement,
import org.openqa.selenium.JavascriptExecutor
But this was not installed,
"org.openqa.selenium.JavascriptExecutor" is not accessed
Import "org.openqa.selenium.JavascriptExecutor" could not be resolved
where to install the module or how to resolve the problem
You have tagged python and using the Selenium-Python clients you don't require any additional import other then from selenium import webdriver as both the methods to execute JavaScript synchronously and asynchronously i.e.
execute_script()
execute_async_script()
are supported out of the box.
Where as incase you are using the Selenium-Java clients you additionally need to import:
import org.openqa.selenium.JavascriptExecutor;

Import statements not working / I think I broke Python / file structure issues?

[I am new to Python (and programming in general) and will definitely say something stupid in this question.]
I had two python programs. In one of them the import statements were working. And in the other one the import statements were not working.
I suspected this had something to do with the file location of the modules relative to the Python files.
It turned out the program that wasn't working was in a sub folder of the program that was working.
So, as an experiment, I tried moving the venv folder into the sub folder where the other program was, but I ended up canceling that once I discovered that I would need to replace some of the files. (Due to the fact that is already had a venv folder.)
Then, as an experiment, I tried renaming the venv folder to "venv1" just to see if the good program would run. I was not surprised when it didn't.
But then I renamed it back to "venv," and it still wasn't working.
from bs4 import BeautifulSoup
import requests
import json, requests
import urllib.request
import bs4 as bs
import urllib
# .... etc ...
output
ModuleNotFoundError: No module named 'bs4'
...
...
...
oh, and if I try:
#from bs4 import BeautifulSoup
import requests
import json, requests
import urllib.request
import bs4 as bs
import urllib
# .... etc ...
Output:
ModuleNotFoundError: No module named 'requests'
I tried pip installing them again (my terminal doesn't recognize sudo pip install) and this is what I got
PS C:\Users\****\Desktop> pip install requests
Requirement already satisfied: requests in c:\users\****\appdata\local\programs\python\python310\lib\site-packages (2.27.1)
I thought maybe I'd look this one up, but the folder "appdata" doesn't exist on my computer, in that location.
What happened and how can I fix it?
The appdata folder should exist in that location. It is a hidden folder, and by default, Windows won't display hidden files/folders. You can view it by pressing WIN+R, and then typing "appdata", and clicking "OK". It should then come up in a file explorer window.
The python packages are installed, but not visible to the scripts. It sounds like you virtual environment may be incorrectly set up. If you open a CMD prompt, and then type in python -m site, it will show you the locations of your python's system path. You should see the install locations for the packages, in this case, you'll probably see the following: C:\\Users\\****\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages.

Unable to import python requests in VSCode

I read this in addition to MANY other things: Importing requests module does not work
I am using VSCode and python 3.8.
I am able to import it seems any library except "requests"
Given the ages of the previous posts I hope to know what a good current next step could be, please and thank you
import math
import asynchat
import signal
import importlib
import requests <-----Will NOT import
response = requests.get("http://api.open-notify.org/astros.json" [api.open-notify.org])
print(response.text)
print(response)
I think your requests is not well installed. Make sure it's installed with the python you are using with.
Try pip3 install requests.

Trouble with Selenium Python related Imports

I'm going to make an application using the Selenium WebDriver (python) and I was having trouble with some of the initial setup. I want to use the page object design pattern so I've been trying to import some materials in this way:
from page_objects import PageObject, PageElementfrom page_objects import PageObject, PageElement
as can bee seen in the documentation here: https://page-objects.readthedocs.io/en/latest/tutorial.html#a-simple-page-object
I'm using PyCharm for development and I've updated selenium and added it to the project interpreter but this import statement still will not work for me. I'm not sure which external module this import relates to or if its just some part of selenium, so any information would be greatly appreciated. Thanks!
That readthedocs is for the page_objects library, the repo for that library can be found here. You need to install that library before you can import it

Python import BeautifulSoup not working

I have a Python script that wants to use BeautifulSoup:
from bs4 import BeautifulSoup
When I run the script from the command line, it works fine. When I run the script externally, from a browser, it dies on that line. The web logs say:
ImportError: No module named bs4
I've also tried, with the same result:
import bs4
import BeautifulSoup
I installed the module from a tarball, and it now resides in a folder that is in my Python path:
/usr/home/myName/.local/lib/python2.7/site-packages/
I have made sure the permissions for the bs4 directory, and all .py and .pyc files in the folder allow execution (chmod 775 *.py), and I've checked to see that both internally and externally the same version of Python is being run (2.7.9 (default, Jan 12 2015, 16:33:18)
[GCC 4.2.1 20070719 [FreeBSD]])
You have not installed BeautifulSoup for the benefit of all the users on your computer. Instead, you installed only for the benefit of the user named "myName". In particular, you haven't installed it for the use of the user that runs the web server (often named 'www' or 'www-data').
If you can, install BeautifulSoup in the system-wide location.
Otherwise, you can modify your script like so:
import sys
sys.path[0:0] = ['/usr/home/myName/.local/lib/python2.7/site-packages/']
from bs4 import BeautifulSoup
Thanks to #robᵩ (phi) for pointing out what was wrong: because this script was on a shared server, I was unable to give execute permission to the "user" running the web server (in this case, "nobody"). I was able to run the script using ssh, since I was the user in that case.
Because I was unable to convince the hosting service to install BeautifulSoup for all users, I ended up using a different library, lxml.html, which my host server does install with Python.
(I found lxml.html a pleasure to work with, as well)

Categories