Because Microsoft will be removing vbScript in the next version of Windows, I have been busy converting my utility vbScripts into Python. Part of this is a library of functions that I keep in D:\include. I'd like to maintain this system in Python, but the import process is rather clumsy. I have to

import sys
sys.path.insert(0,r'D:\include')

Only after that can I import a file. The documentation is somewhat obtuse but I gather my best option is to add the file D:\include__init__.py. As long as that folder is known to Python I could then do

from include import myfile1

to include the file D:\include\myfile1.py. The only problem is that I have yet to find a way to permanently add the include folder to the list of folders that Python searches. I'm sure there is a simple way. Anyone know what it is?

To be more specific - let's say I have a file

D:\include\getargs.py

which contains a function

def getargs(options=[]):

What I want is be able to code in other Python files something like

from include import getargs
.
.
.
named,unnamed,unknown = getargs("infile.txt /verbose outfile.txt")

I set PYTHONPATH to d:\include and I can do

from include import getargs

or

import getargs

but I'd have to refer to the function as getargs.getargs(...). If I want to avoid the double name I have to do

from getargs import *

but I'd prefer to have an import statement that includes the word include to make it easier (grep) to identify my includes.

grep -i "from include import" *.py
commented: from include.getargs import getargs +0

Hmm, this reminds me of our old friend DEFINE. I wonder if Python does that. Off to check that out. It won't obviate a need to change from getargs totally but here I go looking for that.

So yes, Python can def() something so it could shorten your getargs.getargs() down to just what you call your function in the def() code.

To me this is a different question than at top so my thought here is that Python is treating your getargs import as an object and what's inside are members or such so this seems to be exactly what should happen. Now us old foggies that like terse stuff may just have to name this getargs to something short if keystrokes are what we want to reduce.

So if the grep is the goal one could cheat and write:
from getargs import * # from include import getargs
But hey, that's cheating.

That may be cheating but it will give me what I want. It's not clean but it is clear.

Your path (i.e. the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since path is a list, you can use the append method to add new directories to the path. For instance, to add the directory /home/me/mypy to the path, just do: import

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.