Importing a file (not a module).

Discussion in 'Python' started by Luis Zarrabeitia, Feb 9, 2009.

  1. Is there any way to "import" a .py file that is not on sys.path?
    I'd like to 'import' a file that resides somewhere on my filesystem without
    having to add it's directory to sys.path. At first, I thought that something like

    my_module = __import__("path/to/file")

    would work, but I was mistaken. Ideally, the path would be specified as a
    relative path from the importer's module, and not from the CWD (that's another
    question, I guess).

    Related question: how could I change the sys.path for this_script.py without
    changing it much? I'd like to have this_script a few more directories on its
    pythonpath, but I don't want to clutter the begining of the script with
    "sys.path.append(...)"s, specially because I need to do that for several
    scripts. I thought of creating a modify_pythonpath.py and import it from all of
    them, but I don't want to keep that script on the global python path nor on any
    of the project folders (it is required by more than one).

    Currently I have a symlink from the original modify_pythonpath.py to each
    project directory, but that seems clumsy.

    --
    Luis Zarrabeitia
    Facultad de Matemática y Computación, UH
    http://profesores.matcom.uh.cu/~kyrie
     
    Luis Zarrabeitia, Feb 9, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Boulet
    Replies:
    2
    Views:
    317
    Stephen Boulet
    Jul 1, 2004
  2. plb
    Replies:
    2
    Views:
    374
  3. Pradnyesh Sawant

    doubt with importing module, given module name

    Pradnyesh Sawant, Apr 9, 2007, in forum: Python
    Replies:
    0
    Views:
    303
    Pradnyesh Sawant
    Apr 9, 2007
  4. -ecosys.com
    Replies:
    4
    Views:
    326
    Calvin Spealman
    Sep 28, 2007
  5. Visco Shaun
    Replies:
    3
    Views:
    273
    Gabriel Genellina
    Apr 22, 2009
Loading...

Share This Page