#πŸ”’ Difficulty running code as a module

48 messages Β· Page 1 of 1 (latest)

ocean vessel
#
paper_annalysis/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ database.py
β”œβ”€β”€ img_annalysis/
β”œβ”€β”€ other/
β”œβ”€β”€ questions/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ cs.py
β”‚   β”œβ”€β”€ passage.py
β”‚   └── question.py
β”œβ”€β”€ test/
β”‚   └── question_test.py
└── __pycache__/

this is my file structure

~/paper_annalysis $ python -m paper_annalysis.question_test.py                                                  /data/data/com.termux/files/usr/bin/python: Error while finding module specification for 'paper_annalysis.question_test.py' (ModuleNotFoundError: No module named 'paper_annalysis'). Try using 'paper_annalysis.question_test' instead of 'paper_annalysis.question_test.py' as the module name.
~/paper_annalysis $ python -m paper_annalysis.question_test
/data/data/com.termux/files/usr/bin/python: Error while finding module specification for 'paper_annalysis.question_test' (ModuleNotFoundError: No module named 'paper_annalysis')

and I get these error while trying to run this as a module,

spiral rampartBOT
#

@ocean vessel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ocean vessel
#
~/paper_annalysis $ python -m paper_annalysis.question_test.py                                                  

/data/data/com.termux/files/usr/bin/python: Error while finding module specification for 'paper_annalysis.question_test.py' (ModuleNotFoundError: No module named 'paper_annalysis'). Try using 'paper_annalysis.question_test' instead of 'paper_annalysis.question_test.py' as the module name.
~/paper_annalysis $ python -m paper_annalysis.question_test
/data/data/com.termux/files/usr/bin/python: Error while finding module specification for 'paper_annalysis.question_test' (ModuleNotFoundError: No module named 'paper_annalysis')
arctic hatch
ocean vessel
#

wait I should have included a .test there

#
~/paper_annalysis $ python -m paper_annalysis.test.question_test
/data/data/com.termux/files/usr/bin/python: Error while finding module specification for 'paper_annalysis.test.question_test' (ModuleNotFoundError: No module named 'paper_annalysis')
~/paper_annalysis $ python -m paper_annalysis.test.question_test.py
/data/data/com.termux/files/usr/bin/python: Error while finding module specification for 'paper_annalysis.test.question_test.py' (ModuleNotFoundError: No module named 'paper_annalysis'). Try using 'paper_annalysis.test.question_test' instead of 'paper_annalysis.test.question_test.py' as the module name.
#

still doesn't work

#

I wanted to run test.question_test.py

arctic hatch
#

ah, your CWD is paper_annalysis

#

generally when you want to run a submodule as a script, you want your CWD to be just outside the top-level package

#

so instead of your current working directory being ~/paper_annalysis, you should cd .. out of your top package

ocean vessel
#

ok

arctic hatch
#

and then you can run the same command

#

(btw python -m will never need .py at the end, that describes a submodule file named py.py, or a subpackage named py/)

ocean vessel
#

hmm

#

this is my first time trying to run a module

#

from paper_annalysis.question import Question ModuleNotFoundError: No module named 'paper_annalysis.question'

arctic hatch
#

the module path you use should follow the same file structure as your scripts

#

since you have question.py inside questions/, or in other words the path is: paper_annalysis/questions/question.py you would write this to run it: python -m paper_annalysis.questions.question

ocean vessel
#

? I am confused

#

wait let me check it out

arctic hatch
#

see your file structure:

arctic hatch
ocean vessel
#

but paper_annalysis.question also has a init. py so shouldn't it work

arctic hatch
#

do you define/import the Question class inside __init__.py?

#

if so, importing the package would work, you just have a missing s at the end

#
from paper_annalysis.question import Question
#                           ^^```
ocean vessel
#

dam

#
from .question import Question
from .passage import Passage , PassageQuestion          
from .cs import CS

is this fine inside questions/inti.py

arctic hatch
#

using relative imports? yes

ocean vessel
#

thanks mate

#

finally it worked, it took me 2 days trying to figure it out

arctic hatch
# arctic hatch using relative imports? yes

as a sidenote, my preferred style is to only use relative imports for sibling submodules, where they're next to each other and don't require more than one . leading period

#

if i need a parent submodule i'd write the absolute import instead, that makes it easier to understand when i inevitably come back to a project and forget my file structure

ocean vessel
#

anything else I should keep in mind

arctic hatch
#

try to make sure you always work with the same CWD when you run modules with python -m path.to.mod, because absolute imports are in a way, relative to where you have your CWD set

#

more technically, absolute imports search through the directories listed sys.path, and when using python -m, the CWD determines the first directory that is added to sys.path

ocean vessel
#

as a side note do you know how I can given a word find related words?

#

I don't think there is a rank

arctic hatch
#

more of a #community-meta question, but see #roles message

Being active in helping others, providing good help, contributing to our projects, and abiding by our rules go a long way towards catching staff attention, and make the server a better place for both beginners and advanced Python devs.
you can't apply for helper directly, but based on activity, staff nominates people that they think are well-fit for the community

ocean vessel
#

@arctic hatch I am trying to analyse questions, I want to for a given question figure which chapter / topic (pre defined) was it from do you have idea how I can go about achieving it

arctic hatch
ocean vessel
#

well removing unnecessary words and then performing the search on base words might help?

#

! close

#

!close

spiral rampartBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.