0

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

1
  • plz show us the code that use another file Commented Aug 10, 2020 at 6:00

2 Answers 2

1

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 *
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

What would this refer to '/path/to/application/app/folder' ? which folder?
still same issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.