473,406 Members | 2,345 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,406 software developers and data experts.

How can I import functions from another python file

Hi,
i have 2 python files in *different directory* , how can I import
python functions from 1 python file to another?

i get this error:
import task
ImportError: No module named task/

Thank you.

Apr 9 '07 #1
3 12065
On 2007-04-09, yi*****@gmail.com <yi*****@gmail.comwrote:
Hi,
i have 2 python files in *different directory* , how can I import
python functions from 1 python file to another?

i get this error:
import task
ImportError: No module named task/

The directory that module is in must by on your python
path in order to import it. You can do it by modifying
sys.path or by setting the PYTHONPATH env variable.
$ mkdir otherdir
$ cat otherdir/amod.py
def afunc():
return 'found'
$ python
>>import amod
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named amod
>>import sys
sys.path.append('otherdir')
import amod
amod.afunc()
'found'
Apr 9 '07 #2
En Mon, 09 Apr 2007 18:06:11 -0300, yi*****@gmail.com <yi*****@gmail.com>
escribió:
i have 2 python files in *different directory* , how can I import
python functions from 1 python file to another?
See the tutorial about modules, packages, and the module search path:
http://docs.python.org/tut/node8.html
You can extend the search path using .pth files:
http://docs.python.org/lib/module-site.html
(but consider using packages instead of cluttering your search path with
infinite entries)

--
Gabriel Genellina

Apr 9 '07 #3
>Hi,
i have 2 python files in *different directory* , how can I import
python functions from 1 python file to another?

i get this error:
import task
ImportError: No module named task/



The directory that module is in must by on your python
path in order to import it.
That's not exactly correct. You *can* import from files that aren't in
your sys.path. What follows is a full-working (with python 2.5)
example. Perhaps ihooks is going to be obsolete at some point, but it
works now. See PEP 302 for more info. (I'm not sure how to modify this
example to work with a newer import mechanism or else I would provide it
to you.)

import os
def writefile(f, data, perms=750): open(f, 'w').write(data) and
os.chmod(f, perms)

foobar = """
print "this is from the foobar module"

def x():
print "This is the x function."

"""

writefile('/tmp/foobar.py', foobar)
# File:ihooks-example-1.py
import ihooks, imp, os, sys
def import_from(filename):
"Import module from a named file"
if not os.path.exists(filename):
sys.stderr.write( "WARNING: Cannot import file." )
loader = ihooks.BasicModuleLoader()
path, file = os.path.split(filename)
name, ext = os.path.splitext(file)
m = loader.find_module_in_dir(name, path)
if not m:
raise ImportError, name
m = loader.load_module(name, m)
return m

foo = import_from("/tmp/foobar.py")

print foo.x
print foo.x()
print foo.x()

You can do it by modifying
sys.path or by setting the PYTHONPATH env variable.
$ mkdir otherdir
$ cat otherdir/amod.py
def afunc():
return 'found'
$ python
>>>import amod
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: No module named amod
>>>import sys
sys.path.append('otherdir')
import amod
amod.afunc()
'found'
--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Apr 10 '07 #4

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

Similar topics

1
by: Steve | last post by:
Hi, I've written a small cgi python script that collects feedback from a webform. I need to pass the values to another python script.. how do I do this? Is this even possible? Thanks Steve
3
by: | last post by:
Hi everybody, I'm sure there's a way to do this, but I can't find it. How can I execute another .py file from my first .py file without using an exec* command? They're both in the same...
6
by: Ian Sparks | last post by:
I have a python file with a number of functions named with the form doX so : doTask1 doThing doOther The order these are executed in is important and I want them to be executed top-down. They...
11
by: Java and Swing | last post by:
Is there some other way, besides SWIG, which will allow me to call functions inside an Ansi C DLL? Example (C): defs.h ------- typedef unsigned long MY_DIGIT; myapp.c
2
by: Kevin | last post by:
i have a python file called pyq which outputs stock quotes, currently i also have a html file that takes stock ticker inputs, i would like to bridge the two by building another program that takes...
2
by: Phoe6 | last post by:
Hi, I have the following directory structure in my project. Base: file1.py file2.py Directory1: file1-dir1.py I am able to import file1 into file2.py What I need to do is, import file1 in...
8
by: Eric_Dexter | last post by:
I was looking for a simple way to load a simple python program from another python program. I tried os.system(cabel) The file name is cabel.py a csound instrument editor.. The error I am...
2
by: KraftDiner | last post by:
I have a class that is defined in a file called MyClass.py How do I use that class in another python script.. import MyClass ? (Does it need to be in a specific location?)
4
by: Michael Malinowski | last post by:
Is there a way to read the directory that the currently running python file is located in? Cheers Mike.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.