Hi

I have several directories ..

ex uselib is one directory with a file usoc.py and def pars has to be called in the file runner.py. I tried to see the existing ways and implement and obviously confused. So please someone let me know the logic behind calling and having the access permissions, so that when i have a reason i can remember.

This is my initial thoughts
in runner.py

import uselib usoc.py 
           usoc.pars()

what if the def pars: in in another class in usoc.py

I am really confused to get the process of access mechanism.

Regards
--P

There are two ways to do this. The first way is to turn any directory with important python code into a module. This can be done by simply adding a file named '__init__.py' to the folder. Then, you can import the code using the from construction.

from uselib import usoc

usoc.pars()

The other way is to put that directory into your module search path (which is a bit crazier and not necessarily as portable).

import sys
if "uselib" not in sys.path:
    sys.path.append("uselib")
import usoc

usoc.pars()

When you're just running things on your machine, that's acceptable, but if you're making a module for distribution, I'd pick the first way.

Hi Ishara,
Thank you very much. Please could you clarify me
1. '__init__.py' you expect this to be empty file just added to the directory to fecilitate the usability ? or you want me to add to the rename the file with "<file_name>__init__.py'. I think the former is my guess but i want to confirm.

2. What is the difference i should make if say def pars(): i am trying to access is a class instead of procedure.
Thanks in Advance

There are two ways to do this. The first way is to turn any directory with important python code into a module. This can be done by simply adding a file named '__init__.py' to the folder. Then, you can import the code using the from construction.

from uselib import usoc

usoc.pars()

The other way is to put that directory into your module search path (which is a bit crazier and not necessarily as portable).

import sys
if "uselib" not in sys.path:
    sys.path.append("uselib")
import usoc

usoc.pars()

When you're just running things on your machine, that's acceptable, but if you're making a module for distribution, I'd pick the first way.

No problem.

1. The former is indeed correct. The '__init__.py' should be alongside whatever other files you like. This lets the interpreter know that the folder is actually a python module, not just some directory on your machine (note, there are other uses for an __init__.py, but they are outside of the scope of this particular question).

2. If you're trying to access a class, you would access it just like any other class from a module. So if you have a class called bicycle, you would simply run myBicycle = usoc.bicycle("red") Then you can use myBicycle like any other instantiated class.

Comments
IsharaComix is prompt and i think Dani users must be glad to have expert like him. Prompt and expert.

Thank you IsharaComix .. Got my answers..

accessing class and procedures from other directories is now clear to me.

Cheers

No problem.

1. The former is indeed correct. The '__init__.py' should be alongside whatever other files you like. This lets the interpreter know that the folder is actually a python module, not just some directory on your machine (note, there are other uses for an __init__.py, but they are outside of the scope of this particular question).

2. If you're trying to access a class, you would access it just like any other class from a module. So if you have a class called bicycle, you would simply run myBicycle = usoc.bicycle("red") Then you can use myBicycle like any other instantiated class.

This question has already been answered. Start a new discussion instead.