-1

Possible Duplicate:
Importing Python modules from different working directory

I want to import a file that is on the following directory:

 E:\edX\cs6.00x\week6\ProblemSet6

I was trying:

import 'E:\edX\cs6.00x\week6\ProblemSet6\ps6'

where ps6.py is the file i wanted imported in the IDLE, but it complains about a sintax error in the last ', how can I fix that?

Community
  • 1
  • 1
vmp
  • 2,370
  • 1
  • 13
  • 17

4 Answers4

4

You can

import sys
sys.path.append("E:\edX\cs6.00x\week6\ProblemSet6")

and then simply

import ps6
alestanis
  • 21,519
  • 4
  • 48
  • 67
1
import sys
sys.path.append("E:\edX\cs6.00x\week6\ProblemSet6\ps6")

import file_in_ps6.py
Vivek S
  • 5,384
  • 8
  • 51
  • 72
0
import imp
foo = imp.load_source('ps6', 'E:\edX\cs6.00x\week6\ProblemSet6\ps6.py')
foo.BlaBla()
savruk
  • 535
  • 6
  • 21
0

One possible solution could be to put a mth suffixed file in your sys.prefix directory.

>>> import sys
>>> sys.prefix
'F:\\F-ProgramFiles\\Python-3.2.3'
>>>

So in my case the directory is 'F:\F-ProgramFiles\Python-3.2.3'. I can create a file named forexample 'mymodules.mth' in this directory (with the suffix .mth) which contains at least two lines with the following syntax:

<module1 name without module filename suffix>
<absolute file path to your module1 file>
<module2 name without module filename suffix>
<absolute file path to your module2 file>
<module3 name without module filename suffix>
<absolute file path to your module3 file>
        .
        .
        .

After that if you restart a new python session, normally you should have the visibility to your module and being able to import your desired module(s).

Regards,

Dariyoosh

dariyoosh
  • 604
  • 1
  • 4
  • 12