0

I am trying to import a file into Python shell. I have read multiple answers on this site to the same question asked by other users, but none of them have worked.

The command I type is:

import filename.py

I have also tried typing:

from filename import *

In IDLE I clicked File -> Path Browser to make sure my file was saved in one of those folders.

Am I doing any steps wrong?

Josiah
  • 91
  • 2
  • 8
  • 1
    I would check the working directory within your session. You can do this by using 'import os' and then 'os.getcwd()' if the directory your script you are trying to import is not listed, you can change the directory within the session using 'os.chdir('path_to_directory_goes_here') hope this helps! – datawrestler Jan 29 '16 at 03:29
  • 1
    Could it be as simple as `import filename` instead of `import filename.py`? – msw Jan 29 '16 at 03:42

1 Answers1

-1

Look here: Importing files in Python?

Credit to Pradyun for his excellent answer:

If you are working in the same directory, that is, b.py is in the same folder as a.py, I am unable to reproduce this problem (and do not know why this problem occurs), but it would be helpful if you post what os.getcwd() returns for b.py.

If that's not the case, add this on top of b.py

import sys
sys.path.append('PATH TO a.py')

OR if they are in the same path,

import sys
sys.path.append(os.basename(sys.argv[0])) # It should be there anyway but still..

Please do further research before posting your answer.

Community
  • 1
  • 1
Jerrybibo
  • 1,315
  • 1
  • 21
  • 27