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

Categories

Have a file test.py, located on?a Directory for eg /pyproject. and in /pyproject have a subdirectory called api which have two subdirectorys called gmail and facebook and in both directory have .py file such as :

/pyproject/api/gmail/abc.py
/pyproject/api/facebook/xyz.py

Have import?abc and xyz py file ?from?test.py:

import api.gmail.abc

Which resulted:

  File "./test.py", line 3, in <module>
    import api.gmail.abc
ImportError: No module named api.gmail.abc

is There any ideas how to import?xyz/abc?from subdirectory to main test.py file?

See Question&Answers more detail:os

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

1 Answer

You need to have an empty __init__.py file in each directory, it's a marker that tells python this folder is a module. I think it should solve the problem for python2.

In python3 imports have been slightly changed, and there you'll also need to use -m command line parameter.

If the file you're trying to run is for example .../pyproject/src/scraper.py You need to set your working directory to the project root .../pyproject/, and run:

python -m src.scraper

In this example I used a longer path to show that sub-directories are delimited by a . instead of /. In your case python -m test should work fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...