0

I have the following structure:

~/git/
~/git/folder1
~/git/folder2

in ~/git/folder1 I have main.py, which imports doing the following:

import folder2.future_data as future_data

which throws the following error:

import folder2.future_data as f_d
ImportError: No module named folder2.future_data

Despite my $PATH containing

user@mac-upload:~$ echo $PATH
/home/user/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/user/git/folder2

Why am I unable to import from folder2 despite it being in my path?

Am I missing something?

2 Answers 2

1

Try putting an empty __init__.py file in each directory (~/git, ~/git/folder1, and ~/git/folder2). Then do export PYTHONPATH=${HOME}/git:$PYTHONPATH (assuming bash shell).

This will also allow you to just set your PYTHONPATH once at the top level and be done with it. If you add more directories (modules) that you need to import, you can just keep adding __init__.py files to your structure (instead of having to constantly modify your PYTHONPATH every time your file/directory structure changes).

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

Comments

0

You can explicitly added the path inside the main.py script before you doing import

import sys
sys.path.append(r'~/git/folder2')
import future_data

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.