Python Forum
Python 3.12 cannot import local python files as modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.12 cannot import local python files as modules
#14
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.
[Image: v9I7ZQ.png]

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.py
Use 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()
Reply


Messages In This Thread
RE: Python 3.12 cannot import local python files as modules - by snippsat - Jul-27-2024, 10:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inserting Python Buttons into KV Files edand19941 3 750 Feb-19-2025, 07:44 PM
Last Post: buran
  How to write variable in a python file then import it in another python file? tatahuft 4 1,158 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  python 3.13 : import whois HansieB 1 791 Nov-30-2024, 02:58 PM
Last Post: snippsat
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 3 8,539 Oct-01-2024, 03:32 PM
Last Post: Alex_Kirpichny
  Trying to generating multiple json files using python script dzgn989 4 2,814 May-10-2024, 03:09 PM
Last Post: deanhystad
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 1,518 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Different Ways to Import Modules RockBlok 2 1,526 Dec-11-2023, 04:29 PM
Last Post: deanhystad
  Newbie question about switching between files - Python/Pycharm Busby222 3 1,570 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  open python files in other drive akbarza 1 1,606 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 3,999 Jul-23-2023, 08:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020