leoScomme leoScomme - 3 years ago 324
Python Question

import error in python script

I have this directory structure:

- program/
- --manage_dataset/
- ----__ init__.py
- ----dir1/
- ------__ init__.py
- ----dir2
- ------__ init__.py
- ----create_patch.py
- --test/
- ---- __ init__.py
- ----launcher.py


in laucher i need to use a function of create_patch.py so i put :

import manage_dataset.create_patch


in my ide (pycharm) work well but when I execute with terminal i have this error:

ImportError: No module named manage_dataset.create_patch


I tried to inser

sys.path.insert(0,path_to_program/manage_dataset)


but nothing change...

Answer Source

As I said in my comment use import create_patch after adding to sys.path
In directory /home/rolf/program/manage_dataset there is a python program called create_patch.py

def print_patch_id():
    print "Printed from create_patch.py"

Accessing it is as follows from my /home/rolf directory:

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.insert(0,'/home/rolf/program/manage_dataset')
>>> import create_patch as cr_pat
>>> cr_pat.print_patch_id()
Printed from create_patch.py
>>> 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download