![]() |
Python 3.12 cannot import local python files as modules - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python 3.12 cannot import local python files as modules (/thread-42497.html) |
RE: Python 3.12 cannot import local python files as modules - sunflowerdog - Jul-26-2024 Quote:I don't understand it either. It may be a Windows-specific issue but I never use the Windows OS. Most likely I will just move everything to a Linux OS instead, seems like setting these sort of things up on Windows is unnecessarily broken and complicated. RE: Python 3.12 cannot import local python files as modules - sunflowerdog - Jul-26-2024 (Jul-26-2024, 07:21 PM)snippsat Wrote: If just understand how stuff work with The issue with all of these approaches is that they require extra setup. Which is not the end of the world, but it's unnecessarily complex & kind of defeats some of the purpose of using a simple language like Python. I would much rather just use an installation of Python that works as intended. Ultimately, like I said previously, I'm probably just going to avoid using Windows and hope 3.12 on Linux does not have these issues. RE: Python 3.12 cannot import local python files as modules - Gribouillis - Jul-27-2024 (Jul-26-2024, 11:44 PM)sunflowerdog Wrote: I'm probably just going to avoid using Windows and hope 3.12 on Linux does not have these issues.I just tried in Linux with a Python 3.12.4 installed with pyenv and I don't have this problem. By the way, the documentation taught me there is a pyenv-win program to get pyenv functionality in Windows OS. You could perhaps try with a version of Python 3.12 installed by pyenv-win to see if it has the same problem as your install of Python 3.12. RE: Python 3.12 cannot import local python files as modules - snippsat - Jul-27-2024 This problem of not understanding how sys.path work can arise on all OS Windows,Linux,Mac.This problem has been asked here or other places many times for all OS. From Python Doc The initialization of the sys.path module search path If want to add some folders permanently it only takes a minute,using PYTHONPATH. ![]() Other ways eg poetry it will make a package automatic with pyproject.toml and test folder. This way will be easier if eg one day want to add package to PyPi,then just poetry publish .Can just ad poetry later if have make a package and it's more serious project like want it on PyPi. G:\ λ poetry new my_package Created package my_package in my_package G:\ λ cd my_package\ G:\my_package λ ls my_package/ pyproject.toml README.md tests/ G:\my_package\my_package λ ls __init__.py __pycache__/ foo/ main.pyUse it two way. G:\my_package λ poetry run python Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from my_package import bar, main >>> >>> bar.answer_to_life() 42 >>> main.main_func() 'Now in main func' >>> exit()Or poetry will create a virtual environment automatic with poetry shell .G:\my_package λ poetry shell Creating virtualenv my-package-Jw3aWtBz-py3.12 in C:\Users\Tom\AppData\Local\pypoetry\Cache\virtualenvs Spawning shell within C:\Users\Tom\AppData\Local\pypoetry\Cache\virtualenvs\my-package-Jw3aWtBz-py3.12 (my-package-py3.12) G:\my_package>python Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from my_package import bar, main >>> >>> bar.answer_to_life() 42 >>> main.main_func() 'Now in main func' >>> exit() RE: Python 3.12 cannot import local python files as modules - Gribouillis - Jul-27-2024 (Jul-27-2024, 10:00 AM)snippsat Wrote: This problem has been asked here or other places many times for all OS.The documentation says Quote:The first entry in the module search path is the directory that contains the input script, if there is one.That is @sunflowerdog 's issue. This entry is not present in his module search path in Python 3.12. This is an abnormal situation. It cannot be corrected by adding permanently a folder in sys.path. RE: Python 3.12 cannot import local python files as modules - snippsat - Jul-27-2024 (Jul-27-2024, 10:16 AM)Gribouillis Wrote: That is @sunflowerdog 's issue. This entry is not present in his module search path in Python 3.12. This is an abnormal situation. It cannot be corrected by adding permanently a folder in sys.path.Maybe,but i would like sunflowerdog to make folder eg C:\code_1 with one file:# hello.py import sys print('Test run a Python file') print(sys.path) print('-' * 25) print(sys.executable)Then from cmd do this here from start no edit. So the problem now for sunflowerdog should be that it don't find C:\\code_1 folder as it dos for me here?
RE: Python 3.12 cannot import local python files as modules - sunflowerdog - Jul-28-2024 (Jul-27-2024, 11:52 AM)snippsat Wrote:(Jul-27-2024, 10:16 AM)Gribouillis Wrote: That is @sunflowerdog 's issue. This entry is not present in his module search path in Python 3.12. This is an abnormal situation. It cannot be corrected by adding permanently a folder in sys.path.Maybe,but i would like sunflowerdog to make folder eg Yes, that is correct.
RE: Python 3.12 cannot import local python files as modules - Gribouillis - Jul-28-2024 By any chance do you have Python-related environment variables set? Print them with >>> import os >>> print({k: v for k, v in os.environ.items() if 'PY' in k}) RE: Python 3.12 cannot import local python files as modules - snippsat - Jul-28-2024 Do this see if it load frozen_importlib_external.PathFindet loads.>>> import sys >>> sys.meta_path [<_distutils_hack.DistutilsMetaFinder object at 0x0000022BD615F800>, <class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]Make a virtual environment as shown before in post under 3. This to see if it same error is here,virtual environment is like new light Python version. Then when environment is active. G:\div_code\project_env (project_env) λ python Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> >>> sys.path ['', 'G:\\Projects', 'G:\\div_code', 'G:\\div_code\\project_env', 'C:\\python312\\python312.zip', 'C:\\python312\\DLLs', 'C:\\python312\\Lib', 'C:\\python312', 'G:\\div_code\\project_env\\Lib\\site-packages'] RE: Python 3.12 cannot import local python files as modules - sunflowerdog - Jul-29-2024 (Jul-28-2024, 02:40 PM)Gribouillis Wrote: By any chance do you have Python-related environment variables set? Print them with I guess not. When I first installed Python I did manually add Python's default script folders into my PATH because it wasn't recognizing pip at first, though I think that's moreso another symptom of the issue than the cause of it.
|