Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

when running a simple python code it complains about main and spec being NONE

I am new to python and I found a very intesting library I need to play with which is only written in python

here us part of my code

from pkgutil import get_data

grammar = get_data(__name__, "thl.lark").decode("utf-8")

Now when I run it I get

Traceback (most recent call last):
  File "/Users/xxx/thl/thl-jupyter-2020/thl/test.py", line 6, in <module>
    grammar = get_data(__name__, "thl.lark").decode("utf-8")
  File "/xxx/.pyenv/versions/3.9.0/lib/python3.9/pkgutil.py", line 620, in get_data
    spec = importlib.util.find_spec(package)
  File "/xxx/.pyenv/versions/3.9.0/lib/python3.9/importlib/util.py", line 114, in find_spec
    raise ValueError('{}.__spec__ is None'.format(name))
ValueError: __main__.__spec__ is None

Any idea what is going on?

question from:https://stackoverflow.com/questions/65946554/when-running-a-simple-python-code-it-complains-about-main-and-spec-being-none

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you run your script with -m flag provided it will work.
It means that your script will be executed as a module rather than a standalone script. I can assume that pkgutil requires a module to work.

python -m your_script.py

There's a great blog post about python modules and the difference between standalone scripts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...