0

I've a main execution python file (F) where I would like to use some services (S)from other python class the folder structure is :

root/a/my file to execute -- (F)
root/b/python class I would like to use -- (S)

how can I call to the requested python class (S) within my file (F)?

thanks.

3 Answers 3

2

This may be too obvious but other answers are missing the need to ensure that module b is in your sys.path. Thus assuming the folder structure in the question and assuming that directory b contains a file called __init__.py alongside the module s.py, then this incantation is needed:

# Add the b directory to your sys.path

import sys, os
parent_dir = os.getcwd() # find the path to module a
# Then go up one level to the common parent directory
path = os.path.dirname(parent_dir)
# Add the parent to sys.pah
sys.path.append(path)

# Now you can import anything from b.s
from b.s import anything
...
Sign up to request clarification or add additional context in comments.

3 Comments

gauden - I've followed your instructions, but during the execution (while the code reaches import statement) I'm getting "ImportError" exception - No Module named (name of the folder I've tried to import), as I said, the sys.path.append did it's job...
I do get the all files stored in the module I'm trying to import therefore the path I'm creating is ok
Thanks for the uptick. Shall I assume that your issue was solved? The comment here implies there may still be problems.
1

Make a example of this question, the following is your current directory tree:

A folder:
  file a.py
  file b.py
B folder:
  file c.py
  file d.py

In c.py , you want to import a.py to your macro. Then you can do this:

import os
# insert 1, 2, 3... but not 0, 0 is your working directory
sys.path.insert(1, "Folder A 's absolute path")
# Here, you can import module A directly    
import a

Comments

0

Create an __init__.py file in the directory you wish to include.

So lets imagine we have two directories, one called src the other called utils.

If you have Main.py inside the src directory, and you wish to use a class named Network inside a file called Connections.py which is located in utils Then you could do the following.

Please note, the same applies to any folder you create that has a *.py file. For example,we can have folder a, b, c and you simply do from a.b.c import Connections, or whatever your file name may be...

1) Create an __init__.py file (it can simply be empty) inside the utils directory, then from your Main.py do the following.

from utils import Connections

my_instance = Connections.Network()

#Then use the instance of that class as so.
my_instance.whateverMethodHere()

The Directory would look like this:

root dir
  - src 
    -__init__.py
    - Main.py

  - utils
    -__init__.py
    - Connections.py

For more details, you can check out the python documentation which goes more in-depth. http://docs.python.org/tutorial/modules.html#packages

As per the link above, a bit more info on python packages, and why we use __init__.py:

When importing the package, Python searches through the directories on sys.path looking for the package subdirectory.

The init.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package or set the all variable, described later.

2 Comments

but how do I include this directory ? I mean what exactly I write after "import..." ?
and the folder hierarchy, how does it work here ?, I mean if Util is located under root/a/b/c/util

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.