[Python-bugs-list] [ python-Feature Requests-692881 ] import files from any directory

SourceForge.net noreply@sourceforge.net
Mon, 12 May 2003 07:50:21 -0700


Feature Requests item #692881, was opened at 2003-02-25 13:37
Message generated for change (Comment added) made by alexanro
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=692881&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Noam Raphael (noamr)
Assigned to: Nobody/Anonymous (nobody)
Summary: import files from any directory

Initial Comment:
Many times I have a .py file with some functions, which
I want to import. However, its functionality is very
specific, so I don't want to copy it to a directory
where it will always be available as a module. If I
start Python from the directory of the file, I can
import it, but this is not always the case. I can use
execfile(), but import is better since it runs the file
in its own namespace.

What I suggest is to extend the import statement
syntax, to allow  "module" to be a stringliteral as
well as (identifier ".")* identifier. When it is a
string literal, the string will be interpreted as the
file name to import. The default name of the module
will be the name which would import the file if its
directory were in PYTHONPATH. If there is no such name,
an exception will be raised if "as" is not used.

For example, say the file with my functions is called
"specific_functions.py" and sits in the directory
"/dir". When I open the Python shell from the directory
/dir, I can now call "import specific_functions". I
suggest that this would do the same thing: "import
'specific_functions.py' ". When I open the Python shell
from another directory, I currently have no simple way
to import the file, but I suggest that I could use the
command "import '/dir/specific_functions.py' ". The
command "from '/dir/specific_functions.py' import *"
should also work, of course.

This has no backward compatibility issues as far as I
can see, and is quite helpful, I think.

----------------------------------------------------------------------

Comment By: Alexander Rødseth (alexanro)
Date: 2003-05-12 14:50

Message:
Logged In: YES 
user_id=679374

Today, chances are quite good that a random downloaded
python-script will function on any computer. This suggestion
would increase the chances that one will get errors
connected to "missing files" (from 0% to some%, which is a
great deal).

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=692881&group_id=5470