0

I read that to use a python file as a module for import, I need to put __init__.py in the directory.

I have the following directory structure:

data_load
    -- __init__.py
    -- rand_data.py

etc 
    -- __init__.py
    -- test.py

In test.py I import a class defined in rand_data and I get the error:

python test.py 
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    from data_load.rand_data import RandData
ModuleNotFoundError: No module named 'data_load'
4
  • any specific ide you are using ? Commented Oct 4, 2019 at 21:43
  • @prashantrana no just a conda installation + terminal Commented Oct 4, 2019 at 21:44
  • please put a backtick ` before and after the word __init__.py if not people will just see init.py in bold characters Commented Oct 4, 2019 at 21:45
  • can you tell how you are testing the functionality Commented Oct 4, 2019 at 22:04

2 Answers 2

1

change to the parent directory of etc and data_load and type

python -m etc.test

This should do the job.

Here a small test case (Assuming you're on a linux machine)

## create the test case
mkdir -p import_issue/data_load import_issue/etc
touch import_issue/data_load/__init__.py import_issue/etc/__init__.py
echo 'print("I am", __name__)' > import_issue/etc/test.py
echo 'from data_load.rand_data import RandData' >> import_issue/etc/test.py
echo 'print("Randdata = ", RandData)' >> import_issue/etc/test.py
echo "class RandData:" > import_issue/data_load/rand_data.py
echo '    pass' >> import_issue/data_load/rand_data.py
#
# now perform the test
cd import_issue
python -m etc.test

The reason why things in your initial example didn't work out as expected is, that you present working directory was probably etc and if you load a python script in etc, then it tries to import load_data relative to the present working directory (etc) and below etc there is no directory named rand_data, that has a file __init__.py in it.

My suggestion to fix is to go up to the common parent directory (This will now be your present working directory) and import etc/test as a module. The reason is, that test.py is in a directory etc with an init.py so you should import it as etc.test and not call it directly. Calling a file, that is a module directly with etc/test.py is not really recommended and can provoke some rare confusing situations.

Sign up to request clarification or add additional context in comments.

1 Comment

I added a test case to my answer and it works as expected. If it doesn't work for you, then there must be a difference / a detail, that makes your scenario behave different. You you try to provide the smallest possible test case that breaks and shaere it with us.
0

instead of running command in etc directory python test.py run the python script in parent directory of etc and data_load.

ie what you need to do in terminal is (considering you are still in etc directory in terminal)

1.    `cd ..`
2.    `python etc/test.py`

this will work.

why are you facing error is because in test.py python is looking in the current directory for data_load module, and since there is no data_load module in etc directory it is giving this error.

how to solve above problem

  1. append the sys.path in code
  2. run the script from project main folder which contains all the package and python module and scripts.

Comments

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.