beautifulsoup4 installation & import using python3 on mac - python

I am going crazy about the installation of beautifulsoup4 on my macbook on which I have Python 3.x installed.
I managed to install beautifulsoup by typing "python3 setup.py install" in the console. After going through some code the console tells me
Installed /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/beautifulsoup4-4.4.0-py3.5.egg
Processing dependencies for beautifulsoup4==4.4.0
Finished processing dependencies for beautifulsoup4==4.4.0
Patriks-MacBook-Pro:beautifulsoup4-4.4.0 Patrik$
But when I run "import bs4" in Python, it keeps telling me
import bs4
File "<frozen importlib._bootstrap>", line 2205, in _find_and_load
File "<frozen importlib._bootstrap>", line 2190, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2132, in _find_spec
File "<frozen importlib._bootstrap>", line 1908, in find_spec
File "<frozen importlib._bootstrap>", line 1884, in _get_spec
File "<frozen importlib._bootstrap>", line 1865, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 864, in spec_from_loader
File "<frozen importlib._bootstrap>", line 905, in spec_from_file_location
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/beautifulsoup4-4.4.0-py3.5.egg/bs4/__init__.py", line 48
'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'
^
SyntaxError: invalid syntax
but as far as I can see I installed the 4..4.0 version of beautifulsoup4.
There seem to be a lot of people how are seeking for a short but clear noobie-tutorial how to install bs4 in Python 3.x on a Mac with OS 10.x
Hopefully someone can help us out...

Actually the pip3 install BeautifulSoup produces a strange error for me:
Collecting BeautifulSoup
Downloading BeautifulSoup-3.2.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/private/tmp/pip-build-prl0vx3e/BeautifulSoup/setup.py", line 22
print "Unit tests have failed!"
^
SyntaxError: Missing parentheses in call to 'print'
But sudo pip3 install BeautifulSoup4 works!

Related

Cannot create .exe with pyinstaller from .py with torchaudio (CPU): AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'

I have a .py script that uses torchaudio (without GPU) to process some sound in Windows. To distribute it, I've used pyinstaller to turn it into a .exe. You can reproduce the issue with this simple script:
import torchaudio
import time
if __name__ == '__main__':
t = torchaudio.transforms
time.sleep(3)
print("Success")
This script correctly runs from a python console python test.py but I want to create a test.exe that works in Windows (without having python installed). I create test.exe by using pyinstaller: pyinstaller test.py. This creates a build/test folder with all the required dependencies (around 1GB). test.exe is located inside that folder but when I double click on it, it fails with the following error:
Traceback (most recent call last):
File "torch\_ops.py", line 501, in __getattr__
op, overload_names = torch._C._jit_get_operation(qualified_op_name)
RuntimeError: No such operator torchaudio::cuda_version
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "torchaudio\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "torchaudio\_extension.py", line 136, in <module>
File "torchaudio\_extension.py", line 121, in _check_cuda_version
File "torch\_ops.py", line 505, in __getattr__
raise AttributeError(
AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'
[11648] Failed to execute script 'test' due to unhandled exception!
The environment uses:
python==3.9.15
torch==1.13.0
six==1.15.0
numpy==1.22.4
scipy==1.6.0
sounddevice==0.4.5
torchaudio==0.13.0
pyinstaller==5.6.2
Note: I tried the same installing torch with cuda ending up with the same error and a build 4 times bigger.
I was able to make the script work. Here are the steps I took to get it to run.
Create a new empty directory and pasted your script in as main.py
py -m venv venv && venv\scripts\activate && py -m pip install --upgrade pip pyinstaller
pip install torchaudio==0.13.0 torch==1.13.0 numpy=1.22.4 sounddevice==0.4.5 six==1.15.0 scipy
pyinstaller -F main.py
Go into venv\Lib\site-packages and copy the entire torchaudio folder and paste it into the top level directory alongside venv and main.py
In main.spec set datas=[('./torchaudio','./torchaudio')]
pyinstaller main.spec
And after compiling the executable runs... it still gives off a few warnings, but it runs and prints the success message.

How to fix a bad upgrade of pip on python3

Before someone slaps my hand, I had installed venv/virtualenv to setup virtual environments but I had other issues so someone suggested I upgrade my pip3 version to the current version (22.0.4 right now)
I somehow messed up both pip and pip3 so hopefully someone can help me.
I tried to upgrade my version of pip on my Ubuntu 16.04 box by running
pip3 install --upgrade pip
I also tried running
pip3 install --upgrade pip3
After upgrading, I kept getting errors when trying to run pip3 so I removed it, planning to install with the get-pip.py script.
However, now when I try to install pip3 using
sudo python3 /tmp/get-pip.py
I keep getting an AttributeError:
$ sudo python3 /tmp/get-pip.py
Traceback (most recent call last):
File "/tmp/get-pip.py", line 27077, in <module>
main()
File "/tmp/get-pip.py", line 135, in main
bootstrap(tmpdir=tmpdir)
File "/tmp/get-pip.py", line 111, in bootstrap
monkeypatch_for_cert(tmpdir)
File "/tmp/get-pip.py", line 92, in monkeypatch_for_cert
from pip._internal.commands.install import InstallCommand
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_internal/commands/__init__.py", line 9, in <module>
...
...
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 5677, in <module>
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1488, in setResultsName
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1491, in _setResultsName
File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1445, in copy
AttributeError: module 'copy' has no attribute 'copy'
Can someone point me to instructions on how to re-install pip/pip3 or how to get around this AttributeError? Hopefully the solution will also keep my old modules that I had downloaded from before too. Thanks
Check if there is a file /tmp/copy.py or directory+file /tmp/copy/__init__.py. Try these commands: sudo python3 -c "import copy; print(copy.copy)" and sudo python3 -c "import copy; print(copy.__file__)"
If any of those exists it will be imported instead of standard module copy because Python prepends the directory of a script (the script is /tmp/get-pip.py, the directory /tmp/) at the beginning of sys.path so it's where Python starts to find modules/packages.

How to use imports from requirements.txt in python

I am attempting to use a python file in a basic web application. I have my two requirements (docx and xlrd) in my requirements.txt file, but when the python file tries to import either of the requirements, it cannot find the module. I am currently working on Repl.it. Pictures are attached.
I have tried to import only in either location, but I either fail to import the necessary packages, or I cannot refer to functions from the necessary file.
Requirements.txt -
xlrd==1.2.0
docx==0.2.4
other
script.py -
import docx
from docx.enum.table import WD_ALIGN_VERTICAL
import xlrd
error -
(continued)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/home/runner/main/urls.py", line 5, in <module>
from main import views
File "/home/runner/main/views.py", line 3, in <module>
from excelToDocx import transfer
File "/home/runner/excelToDocx.py", line 1, in <module>
import xlrd
ModuleNotFoundError: No module named 'xlrd'
exit status 1
You need to install the packages using the command pip install -r requirements.txt. If they aren't installed, it isn't going to find them.
The requirements.txt file is simply a way to tell users of your package what requirements there are for your program, and then let them install all of them easily through PyPi using the pip program. It doesn't actually tell python to install them for you.

Python / PIP broken on Ubuntu 16.04

I think my Python / PIP environment on a Ubuntu 16.04 machine is broken. I just want to install a program using PIP using this command:
pip3 install --user ocrmypdf
But it doesn't work. When running
pip3 --version
I get this error:
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 7, in <module>
from pip import main
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 59, in <module>
from pip.log import logger
File "/usr/lib/python3/dist-packages/pip/log.py", line 9, in <module>
import colorama, pkg_resources
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
File "/usr/share/python-wheels/setuptools-3.3-py2.py3-none-any.whl/pkg_resources.py", line 1479, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
Python itself seems to work:
root#host /usr/lib # python -V
Python 3.6.3

python external motionless library running error

After the installation of motionless library, I try to run my code and the following error message occurs.
**Traceback (most recent call last):
File "C:\Users\Kevin\Downloads\tracker.py", line 4, in <module>
from motionless import CenterMap
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2222, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 2164, in _find_spec
File "<frozen importlib._bootstrap>", line 1940, in find_spec
File "<frozen importlib._bootstrap>", line 1916, in _get_spec
File "<frozen importlib._bootstrap>", line 1897, in _legacy_get_spec
File "<frozen importlib._bootstrap>", line 863, in spec_from_loader
File "<frozen importlib._bootstrap>", line 904, in spec_from_file_location
File "C:\Python34\lib\site-packages\motionless-1.1-py3.4.egg\motionless.py",
line 55
if label and (len(label) <> 1 or not label in Marker.LABELS):
^
SyntaxError: invalid syntax**
Motionless hasn't been updated since 06/08/2010 according to the PyPi Package Index.
I've downloaded it and get the same error immediately just by running:
import motionless
print(motionless.__version__)
It's also not flagged as being Python 3.4 compatible in PyPi; if you are running the latest version of Python this is likely the issue. Have you tried running it with Python 2.7 instead?
Edit: Looking at the Python 2.7 docs; it states here that != and <> are equvilent, however <> is deprecated. In the Python 3.4 docs it states here that only != is supported, no mention of <> so I imagine it's been removed.
You could try instead:
Raising an issue on the GitHub Repo; the author may still be updating the library and not know it's incompatible with Python 3.4
Checking out the code yourself from the GitHub Repo and manually fixing the problem (Check out 2to3 for automatically doing this. It will convert all <> usage to != for you)

Categories