473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to import from a file which is not in the current directory?

Hi,

I would like to import a function from a file which is located not in
the same directory as the main program (from which the function needed
to be imported).

Could anybody pleas tell me how to do that?

Thank you in advance.
Oct 22 '08 #1
3 987

On Oct 22, 2008, at 5:38 PM, Kurda Yon wrote:
Hi,

I would like to import a function from a file which is located not in
the same directory as the main program (from which the function needed
to be imported).

Could anybody pleas tell me how to do that?
Python will search for module files in a number of places:
http://docs.python.org/tutorial/modu...le-search-path

sys.path will give you a list of all the places that will look for
modules.


Oct 22 '08 #2
Lets say that you are in directory "C:\\Python25" and you want to
import function "bar" defined in module "foo" located in your windows
desktop.
You do it like this:
>>import sys
sys.path.append('C:\\Users\\You\\Desktop')
from foo import blah
So you add your desktop to "sys.path" and then import the function as
though it is in the same directory.
Hope this helps...

Luis

On 22 oct, 18:38, Kurda Yon <kurda...@yahoo.comwrote:
Hi,

I would like to import a function from a file which is located not in
the same directory as the main program (from which the function needed
to be imported).

Could anybody pleas tell me how to do that?

Thank you in advance.
Oct 23 '08 #3
Lets say that you are in directory "C:\\Python25" and you want to
import function "bar" defined in module "foo" located in your windows
desktop.
You do it like this:
>>import sys
sys.path.append('C:\\Users\\You\\Desktop')
from foo import bar
So you add your desktop to "sys.path" and then import the function as
though it is in the same directory.
Hope this helps...
Luis
On 22 oct, 18:38, Kurda Yon <kurda...@yahoo.comwrote:
Hi,

I would like to import a function from a file which is located not in
the same directory as the main program (from which the function needed
to be imported).

Could anybody pleas tell me how to do that?

Thank you in advance.
Oct 23 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Colin Brown | last post by:
I have instances where Python 2.3.2 import both does not work or error. It can be demonstrated under Win2k and Linux by doing the following: 1. Create a subdirectory "abc" 2. In directory "abc"...
97
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will...
0
by: atlantis | last post by:
Hi, I have a very strange problem with xsl:import when usig RELATIVE path on AIX 5.2 server. I have two XSL files in the same directory: "ists_xslt3.xsl" and "ists_xslt3_layout.xsl". This...
10
by: py | last post by:
I have a python script that I want to test/debug. It contains a class which extends from some other class which is located in some other python file in a different directory. For example: ...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
1
by: jgscott3 | last post by:
Dear Access Developers: I need to automatically import a file into Access from the same directory every day. However, I need the code to search the files in the directory and only import the...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
0
by: A3AN | last post by:
Hi. I receive a database backup on a daily basis. I then import this dump on another server which I use for software development. There is two db's being hosted on this server. We test software...
0
by: Laszlo Nagy | last post by:
ohad frand wrote: So this is what you have: /1/tmp1.py /1/tmp2.py /2/tmp1.py /2/tmp2.py
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.