Importing modules in embedded python - 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

Related

How can I import functions from other folders in Python project?

I cannot import the functions from other packages in my Python project.
As suggested, I added
sys.path.append(os.path.abspath('../ExportExcel'))
from ExportExcel.export_excel import export_excel
at the beginning of the clip_finder.py.
I also put
sys.path.append(os.path.dirname(__file__)
inside the export_excel.py.
Inside ExportExcel.init.py I put
from .export_excel import export_excel
However I still get this error:
C:\Main\SupportScripts\ClipFinder>python clip_finder.py -h
Traceback (most recent call last):
File "C:\Main\SupportScripts\ClipFinder\clip_finder.py", line 12, in <module>
from ExportExcel.export_excel import export_excel
ModuleNotFoundError: No module named 'ExportExcel'
How can I make it work?

no module named "3rd party library" in subfolder

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!

How to import self-made modules?

I created some simple modules for learning purposes containing simple classes and functions and tried to import it for the shell. Unfortunately TraceBack Error occurs.
I've checked os.getcwd() and I'm in the directory with all those modules. I also created modules with both pycharm and shell itself (open("filename","w") and stuff) and still python cannot find them.
Do you have any idea what's the reason of such matters?
import komendy
Error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'komendy'
They are in same folder:
>>> os.listdir()
['komendy.py', 'zmienne.py', 'venv', 'mordeczko.txt', 'mordo', '.idea']
Ideas?
You can import your library and its methods like this:
from komendy import *
Also check this:
How to import other Python files?

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.

Can't reload module that is in another folder but also in sys.path

I'm having some troubles reloading a module that is in another directory but is in sys.path.
>>> from module_from_another_dir import *
>>> from importlib import reload
>>> reload(module_from_another_dir)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'module_from_another_dir' is not defined
So as you can see I can import it, but I can't reload it later.
When you import module like this
from module_from_another_dir import *
then Python knows only new methods and "forgets" about module name. However, you can import your module as
import module_from_another_dir
and you will be able to reload it easily.

Categories