no module named "3rd party library" in subfolder - python

My folder structure is as follows.
main
main.py
library
utils.py
utils.py has the following code
from aws_requests_auth.aws_auth import AWSRequestsAuth
def testAuth():
print('some code code')
main.py has the following code
from library.utils import testAuth
def start():
print ('some code')
I have run pip install aws_requests_auth in both my main and library folder.
When i tried to run python3 main.py I got this error
Traceback (most recent call last):
File "main.py", line 1, in <module>
from library.utils import testAuth
File "/Users/Docs/library/utils.py", line 1, in <module>
from aws_requests_auth.aws_auth import AWSRequestsAuth
ModuleNotFoundError: No module named 'aws_requests_auth'
How do i ensure 3rd party libraries are installed properly for sub folders? Thanks!

Related

Importing modules in embedded python

I'm trying to get module imports to work in embeddable python, but it doesn't want to work
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\script.py
from module_test import test
print("Hello world!")
print(test())
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\module_test.py
def test():
return "Test!"
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>#python.exe run_scripts\script.py
Traceback (most recent call last):
File "C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32\run_scripts\script.py", line 1, in <module>
from module_test import test
ModuleNotFoundError: No module named 'module_test'
Why is the module not being imported? I tried changing PYTHONPATH but it didn't help

unittesting fails on ModuleNotFound in the file to be tested imports

I am writing a small unittest for my method move(). The application is running but when I do python -m unittest app.test.test_move_items, it fails with the following error:
ImportError: Failed to import test module: test_move
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/Users/daniel/app/test/test_move.py", line 13, in <module>
from app.move_items import move
File "/Users/daniel/apps/app/move.py", line 29, in <module>
from helpers.calculator import determine_move_amount
ModuleNotFoundError: No module named 'helpers'
If i do python app/move.py, the output is correct.
The folder structure is as follows:
app
helpers
|-calculator.py
move_items.py
test
|-test_move_items.py
move_items.py
from helpers.calculator import determine_move_amount
def move(list_of_items, move_by):
pass
There is init.py file in app and the test directory as well.

Python error - “ImportError: No module named”

First of all this is what my directory looks like:
.:
ref.py main.py utility
./utility:
file_manip.py
When I execute main.py I get this error:
>>> python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
import utility.file_manip as fm
ImportError: No module named utility.file_manip
I cannot for the life of me get to the bottom of this. Clearly utility.file_manip exists...
You need a blank __init__.py file in your utility directory in order for Python to see it as a package. See the tutorial.

importing a file as a package - Python

I have the following directory structure:
/testlib
__init__.py
ec2.py
unit/
__init__.py
test_ec2.py
utils/
__init__.py
I'm trying to create a unittest class for ec2.py:
import ec2
class TestEC2(unittest.TestCase):
def setUp(self):
self.ec2obj = ec2.EC2(name="testlib_unittest")
if __name__ == '__main__':
unittest.main()
However, when I execute test_ec2.py I'm getting the following error:
python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import ec2
ImportError: No module named ec2
I still don't understand why I'm getting that, since I have the __init__.py properly set in the directory. The __init__.py files are completely empty: I've created them with touch __init__.py. Any clues?
**** Updates ****
Here are some suggestions:
/testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
from ..ec2 import EC2
ValueError: Attempted relative import in non-package
testlib# python unit/test_ec2.py
Traceback (most recent call last):
File "unit/test_ec2.py", line 4, in <module>
import testlib.ec2 as ec2
ImportError: No module named testlib.ec2
It can't find the module because you're running the script incorrectly. Run the following in testlib/:
python -m unit.test_ec2
Python is not looking for files in directories above yours. Ignacio's answer is correct, and see this for elaboration.

Basic python questino about import

I am trying to run some Python (2.7.6) code, and the compiler can't find the "util" module.
Here is the line from the code:
from util import getArgs, write
And the error is this:
Traceback (most recent call last):
File "rs.py", line 11, in
from util import getArgs, write
ImportError: No module named util
I can't find this util module anywhere, and pip install does not recognize util, getArgs, and write. How can I fix this problem?
As some of the comments have pointed out, it should be utils (plural) and not util
from utils import <your package>

Categories