4

Current Location: ProjectName/src

Classes Location: ProjectName/Factory Modules/Factory Classes

Try 1:

from FactoryClass1 import FactoryClass1

Try 2:

import sys
sys.path.append(path_to_classes_folder)
from FactoryClass1 import FactoryClass1

However I keep getting 'ImportError: No module named PointSet'.

How should the import statement be written in order to be able on using the functions in the classes?

Elia
  • 762
  • 1
  • 12
  • 28
  • And yes, I did search first before posting but haven't managed to get it to work yet. – Elia Apr 04 '15 at 14:47
  • Does the `Project Name` directory contain only Python sources? If so, you might consider converting it into a package. How are the Factory Modules and Classes expected to be used? Yes, you can screw around with the `sys.path` but unless you know what you're doing, you could be really screwing up your future scalability. Give more information about the contents of `Project Name`. Where is your `main` located? – OozeMeister Apr 04 '15 at 15:00
  • Project Name contains other file formats as well (txt, pts, csv, dxf...) I'm supposed to simply import the classes as modules and use the functions they contain.. Main is located in src/TryNewFunctions – Elia Apr 04 '15 at 15:13

1 Answers1

1

You may try something like:

import os.path, sys
# Add current dir to search path.
sys.path.insert(0, "dir_or_path")
# Add module from the current directory.
sys.path.insert(0, os.path.dirname(os.path.abspath(os.path.realpath(__file__))) + "/dir")

Which will add your directory to Python search path. Then you may import as usual.

To see which paths has been added, check by:

import sys
from pprint import pprint
pprint(sys.path)

If still doesn't work, make sure that your module is the valid Python module (should contain __init__.py file inside the directory). If it is not, create empty one.


Alternatively to just load the classes inline, in Python 3 you may use exec(), for example:

exec(open(filename).read())

In Python 2: execfile().

See: Alternative to execfile in Python 3.2+?


If you run your script from the command-line, you can also specify your Python path by defining PYTHONPATH variable, so Python can lookup for modules in the provided directories, e.g.

PYTHONPATH=$PWD/FooDir ./foo.py

For alternative solutions check: How to import other Python files?

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
  • I did try it, got the first path to be: 'C:\\Users\\##\\workspace\\Project_Name\\src\\Factory Modules\\Factory Classes'' I still get the same error of 'ImportError: No module – Elia Apr 04 '15 at 15:11
  • @Elia Maybe your module isn't the module. Make sure that the added path of your module contains `__init__.py`. – kenorb Apr 04 '15 at 15:16
  • Well, none of the folders contain `__init__.py` If I create new `xx.py` inside the class folder and then import it using: `import FactoryClass1` then everything seems good. However I can't manage to import it from `xx.py` file outside that folder. – Elia Apr 04 '15 at 15:23
  • @Elia Every module imported from Python should contain `__init__.py`. Also remember all your paths in `sys.path` should be absolute, so maybe you're using relative paths. If you've some specific non-working case, re-edit question with more details about location of your classes (in which case it doesn't work). – kenorb Apr 04 '15 at 15:38
  • I meant to say each module contains `__init__` inside the class but not as a seperate file. – Elia Apr 04 '15 at 15:40
  • @Elia You should create empty files then for each directory/module. – kenorb Apr 04 '15 at 15:41
  • 1
    Well... that doesn't make any sense but it worked.. Thank you @kenorb – Elia Apr 04 '15 at 18:46