Python Forum
import just one class from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import just one class from a file
#1
Hi all ! In a file called " __init__.py" I have many classes. I am only interested from the "class sudoku". In my "__main__.py" I write , at the top, "from __init__ import sudoku". That does not work. "from __init import **sudoku**", does not work.
"from __init__ import class sudoku"; None works. Thanks for your help.
Reply
#2
Your explanation dos not make so much sense,so i make a example.
C:\Python36\sudoku_folder\
  |-- __init__.py
  |-- bar.py
  game\
    |-- __init__.py
    |-- sudoku.py
__init__.py files are blank,they help Python navigate the file tree.
# bar.py
def foo():
    return 42

if __name__ == '__main__':
    print(foo())
# sudoku.py
class Sudoku:
    def __init__(self, number):
        self.number = number

    def result(self, last_number):
        return last_number + self.number

if __name__ == '__main__':
    play = Sudoku(100)
    print(play.result(50))

Now can i test my package.
C:\
λ ptpython
>>> from sudoku_folder.game import sudoku

>>> play = sudoku.Sudoku(555) # Now do i access Sudoku class 
>>> play.result(100)
655

>>> # Assess the lonely foo() function
>>> from sudoku_folder import bar

>>> bar.foo()
42
Try to understand the basic how folder and files work in a package.
Can also edit __init__.py to shorten import statement,but that do i not take now.
Reply
#3
Name of folder: sud
Contains: __pycache__, __init__.py, __main__.py
In __main__.py I wrote: from sud.__init__ import sudoku
No error appears.
Reply
#4
Rename __main__.py to a normal name related to contend.
Only __init__.py shall have __ double underscore in you package folders.
Quote:from sud.__init__ import sudoku
__init__ shall never be part of import statement.

Take a step back,because you mess this up bye not understand how it work.
Look at my code one more time,take it for a test run.
Look at Packages documentation.
Reply
#5
I think you are right for the name of __init__.py. But the author of the sudoku solver, did so. I am amazed to see that it is possible to work just on a class. If you want information on the sudoku, look at my thread "Continue SUDO project", in GUI forum. Thanks for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,670 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 803 Jan-24-2023, 02:48 PM
Last Post: demdej
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,816 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 3,082 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Python - Import file sequence into Media Pool jensenni 1 2,082 Feb-02-2021, 05:11 PM
Last Post: buran
Smile Import error with local file colt 1 1,878 Nov-08-2020, 07:56 AM
Last Post: Gribouillis
  Code import .CSV file to MySQL table rtakle 4 2,803 Apr-30-2020, 03:16 PM
Last Post: anbu23
  Regarding import library in two different program file Rohit 3 2,408 Jan-22-2020, 07:14 AM
Last Post: buran
  How to import the name of the fluid in CoolProp from an yml file termo 0 1,800 Sep-26-2019, 01:32 PM
Last Post: termo
  How do you import a dictionary from a .csv file? SteampunkMaverick12 2 2,176 Jul-29-2019, 09:20 AM
Last Post: sudheerm

Forum Jump:

User Panel Messages

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