ModuleNotFoundErro--but I was already importing it - python-3.x

I'm trying to run a cron job from my local machine using crontab. The file I'm trying to run is working without error when run manually from the command line.
The job looks pretty much like this:
01 13 * * * cd path/to/file && echo 'password' | sudo -S /usr/bin/python3 filename.py > log.txt 2>&1
Here's the error I was getting:
Password:Traceback (most recent call last):
File "contractsfinder.py", line 3, in <module>
from scraper_functions import *
File "</path/to/file>/my_functions.py", line 3, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
The file my_functions.py is just where I keep all my import requests in a central document accessed by a number of other python scripts. I am importing a number of modules. There is no issue with loading any of the modules when I am running this script manually.
I saw on another post somewhere that I should try to add: from urllib import requests which I did, and that got rid of the original error saying 'no module named requests'. However, now I'm getting a new error: ModuleNotFoundError: No module named 'bs4'
Trying the fix that worked for requests doesn't work for bs4 because there is no library to import it from, or at least I don't think there is. Previously I had just used import bs4.
So, obviously when I am running the file this way, something different needs to happen with the importing of modules. But when I did pip3 list earlier I saw both bs4 and requests were in there all along.
What do I need to do differently to get these modules to import correctly?

Related

gRPC generated code for Python 3 not working

protoc was generating invalid code in Python 3.8. I found that it works in Python 3.6.
I followed the instructions as per: This fantastic blog
The command I ran was:
python -m grpc_tools.protoc --python_out=src/ --grpc_python_out=src/ --proto_path generated=./src/interfaces src/interfaces/ecg.proto
The first issue was the generated imports were not working. I changed the code as below, for the import to work, in pb2_grpc.py:
from . import ecg_pb2 as generated_dot_ecg__pb2
The imports were fine in Visual Code, but now the run-time error.
I have a couple of files in the "generated" folder. I also added an init.py into this folder that was supposed to expose the contents, as:
from .ecg_pb2 import *
from .ecg_pb2_grpc import *
At a peer level to this "generated" folder I have another file - server.py. I am trying to import "generated". It is only importing with a relative path: from .generated import ecg_pb2_grpc
However, while the import works with the relative path, the main in server.py is not getting invoked. Run time error -
(p36) C:\ECG\src>python ecg_server.py
Traceback (most recent call last):
File "ecg_server.py", line 6, in
from .generated import ecg_pb2
ModuleNotFoundError: No module named 'main.generated'; 'main' is not a package

Unable to run python code from Linux command line

I have gone through the explanations given in this forum and have tried them in my program. However, none of the suggestions worked. That's why I am opening this thread.
Below is the tree for my project. There are 2 packages: com and main.
When I try to run the code for ProcessRiskModelbyRecordID.py from command line, I am getting below error message:
$ python3 /AppDev/XXXX/py/riskScore/main/ProcessRiskModelbyRecordID.py
Traceback (most recent call last):
File "/AppDev/XXXX/py/riskScore/main/ProcessRiskModelbyRecordID.py", line 6, in
from main.ConnectAPI import *
ModuleNotFoundError: No module named 'main'
When I run the same code from PyDev, I am able to execute it.
Below is the import code from ProcessRiskModelbyRecordID.py:
from main.ConnectAPI import *
from com import DBOperations as DBO,SourceProfile,TargetProfile
Can you please help so that I can run this code from command line?
PyDev is probably setting PYTHONPATH for you. On the command line you would need to set it yourself:
cd riskScore
export PYTHONPATH=`pwd`
python3 main/ProcessRiskModelbyRecordID.py

I can't install request module in python

I'm new to python. I'd like to use requests module. but when i type in $, the system shows me "invalid syntax" Is the problem lays in the $?
$ pip install requests
Returns:
SyntaxError: invalid syntax
import requests
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
You need to run
pip install requests
in the command line.
For Windows, the command line would be called cmd or PowerShell.
For MacOS, it would be called Terminal.
and for Linux, it would be called Shell or Terminal. (in which case you might need to call pip3 instead).
From there, you can do
import requests
in any Python file.

How can I import bs4 from Beautiful Soup in Python3 on OSX 10.12.5?

I'm trying to fix what seems like a common problem with importing a module in Python 3. I'm running OS X 10.12.5 and have Python 3 installed on my MacBook Air and am using Sublime Text to edit and run my code.
When I try this import:
from bs4 import BeautifulSoup
...I get this error:
Traceback (most recent call last):
File "/Users/<myname>/Python/code-python3/Pgm#001", line 5, in <module>
from bs4 import BeautifulSoup
ImportError: No module named 'bs4'
I successfully installed with this PIP and on every re-install I see this:
$ pip install beautifulsoup4
Requirement already satisfied: beautifulsoup4 in ./anaconda/lib/python3.5/site-packages
I've tried qualifying the location with things like:
from ./anaconda/lib/python3.5/site-packages/bs4 import BeautifulSoup
...but invariably get a variety of syntax errors; usually on the first '/'.
I am not using a virtual environment but do plan to read up on that approach as these kind of configuration and setup errors are big time wasters.
When I try to run this right in python3 I get slightly different errors:
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/<myname>/anaconda/lib/python3.5/site-packages/bs4/__init__.py", line 30, in <module>
from .builder import builder_registry, ParserRejectedMarkup
File "/Users/<myname>/anaconda/lib/python3.5/site-packages/bs4/builder/__init__.py", line 314, in <module>
from . import _html5lib
File "/Users/<myname>/anaconda/lib/python3.5/site-packages/bs4/builder/_html5lib.py", line 70, in <module>
class TreeBuilderForHtml5lib(html5lib.treebuilders._base.TreeBuilder):
AttributeError: module 'html5lib.treebuilders' has no attribute '_base'
Any tips about where the obvious answer exists that I am still missing would be greatly appreciated. I've seen a lot of things about PATH and PYTHONPATH in similar questions but had no success with any of these solutions either.
Yes, I'm not a fan of anaconda but looks like the path is being pointed to:
"/Users//anaconda/lib/python3.5/site-packages"
... and for the sake of keeping things as clean as possible (this being a good example/reason why you'll see people say to use env for your python projects)
Lets google here! Here's how to remove anaconda:
conda install anaconda-clean #first the configs
anaconda-clean --yes
rm -rf ~/anaconda.* #then conda, the ".*" is regex to cover any version
The check your bash_profile and/or bashrc; and here you can set your Python path. Make sure it not pointing to anaconda
if you run
ls -lh `which python # orpython3
You'll get the path of which is the one you'll be setting as the path.

Imports not found when running script outside of Pycharm?

I have a project structured this way...
main.py imports scripts from subfolders like so:
from controllers.available_balances_controller import available_balances_controller
Subfolders:
models
views
controllers
When running main.py in Pycharm it works find.
When I try to run in terminal I get import errors:
Traceback (most recent call last):
File "main.py", line 6, in <module>
from controllers.available_balances_controller import available_balances_controller
ImportError: No module named controllers.available_balances_controller
Am I importing the scripts wrong in main.py?
What is the proper way to do the importing?
Try running your script with the -m flag:
$ python -m main
That means that you are running your main.py as a module inside a python package, not as a simple script. PyCharm makes it easy for you by assuming so when you create a project. When you are in the terminal, you need to specify it yourself. You don't need __init__.py files inside your directories in Python3.
Check out:
https://docs.python.org/3/reference/import.html
Relative imports in Python 3

Resources