1

I'm not sure where I'm going wrong, I've tried a bunch of the methods listed in other questions, so I'm going to re-ask in case I'm missing something.

I have the following structure:

|-bin/
  -file.py
|-unittests/
  -__init__.py
  |-test_bin/
    -__init__.py
    -test_file.py

I've tried the following inside test_file.py to no avail:

1) Import Error: No module named bin.file

from bin.file import *

2) Import Error: No module named bin.file

import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from bin.file import *

3) ValueError: Attempted relative import in non-package

from ...bin.file import *

The command I'm using is python test_file.py

3 Answers 3

2

For your import to work you need to be at the root of your project.

However, you can add a setup.py file at the root of your package which will allow you to import your functons from anywhere (see below):

Setup.py :

from setuptools import setup

setup(
    name='test',
    version='0.1',
    packages=['bin']
)

and then you run the following shell command at the root of your project:

python setup.py develop

enter image description here

With these steps you should be able to import your file as wanted.

Note that the test.eff-info folder is automatically created.

I hope it helps

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

4 Comments

Note that python setup.py install works too. The difference is in the fact that the modifications in your notification are automatically applied or not.
Thanks, I needed to run python setup.py develop --user, now I'm hitting another problem though, as file.py is no longer able to import its modules after being imported into test_file.py
I'm not sure to understand your second problem. Have you tried from bin.file import [name_of_the_function]
Nevermind - it's a complete unrelated problem to how the code is structured. Your solution helped me with the question of importing a module.
0

Python imports are a bit weird. First, to fix your problem- add an empty file called __init__.py to your bin directory. Now to explain why-

Python builds up a search path. It uses all directories in the environment variable PYTHONPATH, plus site-packages for your virtualenv or your base interpreter. Generally, running python will also add the current working directory to PYTHONPATH.

So any .py file in one of those directories can be imported. But for modules, it doesn't look at EVERY directory. Instead, it only counts directories that contain that magic __init__.py file. So you would have seen bin.py there.. but not bin/bin.py, until you add the __init__.py file.

(this goes for nested directories all the way down).

Looking at your question, this may still be an issue- if you're running your command from the bottom directory where test_file is, your root directory isn't on the search path to see bin anyway. So you would have to do PYTHONPATH=../..:$PYTHONPATH python test_file.py or run the command from the root python unittests/test_bin/test_file.py

2 Comments

from python 3.3+ __init__.py is not necessary.
That's interesting- Guess I had gotten so used to debugging that issue in 2.7 I didn't realize they stopped happening!
0

Add the code below at the beginning of the test_file.pyIt will get current dir of your current file, then find the sub dir which is unittests and finally it will reach parent dir bin.

Also, it's good to change the name of file => file.py to another name to avoid conflicts.

Try the code below and let me know.

import os,sys,inspect
current_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
unittests_dir = os.path.dirname(current_dir)
bin_dir = os.path.dirname(unittests_dir)
sys.path.insert(0, bin_dir)

import file 

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.