3

I have a file call entryPoint.py :

from .commonLib.deviceLib import *

And I have a file called deviceLib.py :

import math
import sys
import logging
import requests
import this

class DeviceLib(object):
    def __init__(self, connectionDb):
        self.__db = connectionDb

The tree is like this :

/test
    entryPoint.py
/commonLib
    __init__.py
    deviceLib.py

When I execute python entryPoint.py I get the error : Attempted relative import in non-package. Please help me.

Menfis Menfis
  • 63
  • 1
  • 3
  • 11
  • 1
    Where are you running entryPoint.py from? Your working directory may highlight the error. – tda Sep 03 '18 at 11:43
  • Possible duplicate of [How to do relative imports in Python?](https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python) – colidyre Sep 03 '18 at 11:43
  • 1
    General advise: It's better to avoid `import *` if you can, not to get your namespace flooded/overwritten (potentially dangerous). Also do you really need `import this`? – colidyre Sep 03 '18 at 11:45
  • Noted @colidyre.... – Menfis Menfis Sep 03 '18 at 11:48
  • Things to try. 1) Add `__init__.py` to /test. 2) Try running `python test/entryPoint.py` with the first `.` removed from the entryPoint.py import statement. – tda Sep 03 '18 at 11:57

3 Answers3

14

use sys.path.append to append the directory where your python file (module is). E.g if your entryPoint.py is inside address directory

import sys
sys.path.append('/path/to/your/module/address/')
import entryPoint
ewalel
  • 1,932
  • 20
  • 25
1

There should be __init__.py in the folder both /test and /commonLib reside.

then just do

from commonLib import deviceLib

For example

sound
|-- effects
|   |-- echo.py
|   |-- __init__.py
|   |-- reverse.py
|   `-- surround.py
|-- filters
|   |-- equalizer.py
|   |-- __init__.py
|   |-- karaoke.py
|   `-- vocoder.py
|-- formats
|   |-- aiffread.py
|   |-- aiffwrite.py
|   |-- auread.py
|   |-- auwrite.py
|   |-- __init__.py
|   |-- wavread.py
|   `-- wavwrite.py
`-- __init__.py

lets assume you are right now opened wavread.py in format subdirecory, you can import karaoke.py from filters by just

from filters import karaoke

More information Here, https://www.python-course.eu/python3_packages.php

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
  • Suppose we have another folder called /sound1 at the same level as /sound and I want to import a file from /sound1. – Menfis Menfis Sep 03 '18 at 11:55
  • Correct me if I am wrong but this will only work if the working directory is `sound/`. If your working directory is `formats/` - this will not work. – tda Sep 03 '18 at 11:55
  • @TD-Asker Thats true. you should be in the main directory where everything else is residing. The reason you can packages then sound and host it into pypi to be installed by pip. once installed by pip, you can be any directory in your computer but be able to import sound and sub scripts in sound – Khalil Al Hooti Sep 03 '18 at 12:09
  • @MenfisMenfis then sound and sound1 should be in a common directory having `__init__.py`. the common directory is the main directory and is your package name. – Khalil Al Hooti Sep 03 '18 at 12:11
  • if I run the script from commandline, without tweaking sys.path, it still complains about `no module of name ...` – kakyo Jun 07 '19 at 10:46
0

To import a file from another directory you can use this code :

import sys
sys.path.insert(0, 'folder destination')
import file

As you can see here we included the path so python will look for the file in that path as well.

  • sys.path.insert is going to add that path so python will look in that directory for your file as well. – amirhosein majidi Sep 03 '18 at 11:50
  • 1
    Yes, or you could use the `import` system? It's not the easiest, for sure, but you shouldn't need to mess with `sys.path` – roganjosh Sep 03 '18 at 11:50
  • Although this is one solution, consider the idea that you may want to conduct multiple relative imports from multiple sub-directories etc. - it's impractical to insert each directory to each script one-by-one... – tda Sep 03 '18 at 12:03
  • About that you can read this : https://docs.python.org/3.4/tutorial/modules.html#the-module-search-path – amirhosein majidi Sep 03 '18 at 12:06
  • Yes, but that isn't the answer you provided. – tda Sep 03 '18 at 12:13