I have a repo that looks like this
dev ---> common ---> utils.py
---> scripts --> upload_to_blob.py
It gives following error when I try to import utils from common
Traceback (most recent call last):
File "scheduled_scripts/upload_to_blob.py", line 6, in <module>
from common import utils
ModuleNotFoundError: No module named 'common'
I am using Python 3.6 env in Anaconda with base Python 2.7
By default, you cannot. When importing a file, Python only searches the current directory, the directory the entry point script runs from, and sys.path which includes locations like the package installation directory (it's actually a little more complicated than that, but that covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, 'scheduled_scripts/upload_to_blob.py')
import file
Have __init__.py
files in your directories and you can access the other files.
dev -
|- common
| |- utils.py
| |- __init__.py
|- scripts
| |- upload_to_blob.py
| |- __init__.py
|- __init__.py
if you want to include from the file, you can use like below
from common.utils import *
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.