Suppose you have installed Python in C drive but you have your projects in D drive. Then in such case you can simply import the python module or file using the following statement-

import file.py

Here we show how to import module located in different directory other than python system path. This is required if you use Python command line prompt or Python IDLE GUI.

Let us suppose that the module file.py you want to import is located in folder python in D drive(d:/python). Then suppose you start Python command prompt and type-

>>> import file

This will throw an error. See below-


 This happens because Python is unable to find the file.py located in unspecified location(D drive). The work around is to specify the path of the location of the python module file. You can specify the path to look for using the following command-

>>> import sys
>>> sys,path.insert(0,'d:/python')
>>> import file



As you can see the statement-
print("great, it's working!")
in the file.py is executed.

The thing applies when you are working with Python IDLE GUI.



0 comments:

Post a Comment

 
Top