Rectangle 27 554

There are many ways to import a python file, all with their pros and c...

Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.

I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:

def what_does_the_fox_say():
  print("vixens cry")

Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>

You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.

Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:

def moobar():
  print("hi")

Put this in /home/el/foo2/main.py:

execfile("/home/el/foo2/mylib.py")
moobar()

run the file:

el@apollo:/home/el/foo$ python main.py
hi

The function moobar was imported from mylib.py and made available in main.py

Put this in /home/el/foo3/chekov.py:

def question():
  print "where are the nuclear wessels?"

Put this in /home/el/foo3/main.py:

from chekov import question
question()
el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?

If you defined other functions in chekov.py, they would not be available unless you import *

Example 4, Import riaa.py if it's in a different file location from where it is imported

  • Put this in /home/el/foo4/stuff/riaa.py: def watchout(): print "computers are transforming into a noose and a yoke for humans"
  • Put this in /home/el/foo4/main.py: import sys import os sys.path.append(os.path.abspath("/home/el/foo4/stuff")) from riaa import * watchout()
el@apollo:/home/el/foo4$ python main.py 
computers are transforming into a noose and a yoke for humans

That imports everything in the foreign file from a different directory.

os.system("python yourfile.py")
import os
os.system("python yourfile.py")

Example 6, import your file via piggybacking the python startuphook:

~/.pythonrc.py
class secretclass:
    def secretmessage(cls, myarg):
        return myarg + " is if.. up in the sky, the sky"
    secretmessage = classmethod( secretmessage )

    def skycake(cls):
        return "cookie and sky pie people can't go up and "
    skycake = classmethod( skycake )
import user
msg = "The only way skycake tates good" 
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
$ python main.py
The only way skycake tates good is if.. up in the sky, 
the skycookie and sky pie people can't go up and  have the sky pie! 
SKYCAKE!

Example 7, Most Robust: Import files in python with the bare import command:

/home/el/foo5/
/home/el/foo5/herp
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

Make a new directory /home/el/foo5/herp/derp

__init__.py
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py

Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:

def skycake():
  print "SkyCake evolves to stay just beyond the cognitive reach of " +
  "the bulk of men. SKYCAKE!!"

The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;

from herp.derp.yolo import skycake
skycake()
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!

The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.

If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

You should also add Example 6: using __import__(py_file_name). Amazing guide anyway

What's the big difference between all of these, and why is one better than any other? For example 5, you write "Import files in python with the bare import command," but you also use the (bare?) import command in examples 1, 3 and 4, don't you?

Hey Eric! I think you got me wrong.. I just wanted to answer HelloGoodbyes question "what's the big difference between all of these" because I too was curious and found the blog entry (which is NOT mine btw) which I thought was helpful for him too...

Good answer but the fact that you use a different import file as example all the times makes it cumbersome to read.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 545

There are many ways to import a python file, all with their pros and c...

Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.

I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:

def what_does_the_fox_say():
  print("vixens cry")

Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>

You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.

Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:

def moobar():
  print("hi")

Put this in /home/el/foo2/main.py:

execfile("/home/el/foo2/mylib.py")
moobar()

run the file:

el@apollo:/home/el/foo$ python main.py
hi

The function moobar was imported from mylib.py and made available in main.py

Put this in /home/el/foo3/chekov.py:

def question():
  print "where are the nuclear wessels?"

Put this in /home/el/foo3/main.py:

from chekov import question
question()
el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?

If you defined other functions in chekov.py, they would not be available unless you import *

Example 4, Import riaa.py if it's in a different file location from where it is imported

  • Put this in /home/el/foo4/bittorrent/riaa.py: def watchout_for_riaa_mpaa(): print "there are honeypot kesha songs on bittorrent that log IP " + "addresses of seeders and leechers. Then comcast records strikes against " + "that user and thus, the free internet was transmogified into " + "a pay-per-view cable-tv enslavement device back in the 20th century."
  • Put this in /home/el/foo4/stuff/riaa.py: def watchout(): print "computers are transforming into a noose and a yoke for humans"
  • Put this in /home/el/foo4/main.py: import sys import os sys.path.append(os.path.abspath("/home/el/foo4/stuff")) from riaa import * watchout()
el@apollo:/home/el/foo4$ python main.py 
computers are transforming into a noose and a yoke for humans
el@apollo:/home/el/foo4$ python main.py 
there are honeypot kesha songs on bittorrent...

That imports everything in the foreign file from a different directory.

Example 5, Import files in python with the bare import command:

os.system("python yourfile.py")
import os
os.system("python yourfile.py")

Example 6, import your file via piggybacking the python startuphook:

~/.pythonrc.py
class secretclass:
    def secretmessage(cls, myarg):
        return myarg + " is if.. up in the sky, the sky"
    secretmessage = classmethod( secretmessage )

    def skycake(cls):
        return "cookie and sky pie people can't go up and "
    skycake = classmethod( skycake )
import user
msg = "The only way skycake tates good" 
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
$ python main.py
The only way skycake tates good is if.. up in the sky, 
the skycookie and sky pie people can't go up and  have the sky pie! 
SKYCAKE!

Example 7, Most Robust: Import files in python with the bare import command:

/home/el/foo5/
/home/el/foo5/herp
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

Make a new directory /home/el/foo5/herp/derp

__init__.py
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py

Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:

Bonus protip, whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

def skycake():
  print "SkyCake evolves to stay just beyond the cognitive reach of " +
  "the bulk of men. SKYCAKE!!"

The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;

from herp.derp.yolo import skycake
skycake()
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!

The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.

If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

You should also add Example 6: using __import__(py_file_name). Amazing guide anyway

What's the big difference between all of these, and why is one better than any other? For example 5, you write "Import files in python with the bare import command," but you also use the (bare?) import command in examples 1, 3 and 4, don't you?

Hey Eric! I think you got me wrong.. I just wanted to answer HelloGoodbyes question "what's the big difference between all of these" because I too was curious and found the blog entry (which is NOT mine btw) which I thought was helpful for him too...

Good answer but the fact that you use a different import file as example all the times makes it cumbersome to read.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 545

There are many ways to import a python file, all with their pros and c...

Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.

I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:

def what_does_the_fox_say():
  print("vixens cry")

Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>

You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.

Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:

def moobar():
  print("hi")

Put this in /home/el/foo2/main.py:

execfile("/home/el/foo2/mylib.py")
moobar()

run the file:

el@apollo:/home/el/foo$ python main.py
hi

The function moobar was imported from mylib.py and made available in main.py

Put this in /home/el/foo3/chekov.py:

def question():
  print "where are the nuclear wessels?"

Put this in /home/el/foo3/main.py:

from chekov import question
question()
el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?

If you defined other functions in chekov.py, they would not be available unless you import *

Example 4, Import riaa.py if it's in a different file location from where it is imported

  • Put this in /home/el/foo4/stuff/riaa.py: def watchout(): print "computers are transforming into a noose and a yoke for humans"
  • Put this in /home/el/foo4/main.py: import sys import os sys.path.append(os.path.abspath("/home/el/foo4/stuff")) from riaa import * watchout()
el@apollo:/home/el/foo4$ python main.py 
computers are transforming into a noose and a yoke for humans

That imports everything in the foreign file from a different directory.

os.system("python yourfile.py")
import os
os.system("python yourfile.py")

Example 6, import your file via piggybacking the python startuphook:

~/.pythonrc.py
class secretclass:
    def secretmessage(cls, myarg):
        return myarg + " is if.. up in the sky, the sky"
    secretmessage = classmethod( secretmessage )

    def skycake(cls):
        return "cookie and sky pie people can't go up and "
    skycake = classmethod( skycake )
import user
msg = "The only way skycake tates good" 
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
$ python main.py
The only way skycake tates good is if.. up in the sky, 
the skycookie and sky pie people can't go up and  have the sky pie! 
SKYCAKE!

Example 7, Most Robust: Import files in python with the bare import command:

/home/el/foo5/
/home/el/foo5/herp
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

Make a new directory /home/el/foo5/herp/derp

__init__.py
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py

Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:

def skycake():
  print "SkyCake evolves to stay just beyond the cognitive reach of " +
  "the bulk of men. SKYCAKE!!"

The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;

from herp.derp.yolo import skycake
skycake()
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!

The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.

If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

You should also add Example 6: using __import__(py_file_name). Amazing guide anyway

What's the big difference between all of these, and why is one better than any other? For example 5, you write "Import files in python with the bare import command," but you also use the (bare?) import command in examples 1, 3 and 4, don't you?

Hey Eric! I think you got me wrong.. I just wanted to answer HelloGoodbyes question "what's the big difference between all of these" because I too was curious and found the blog entry (which is NOT mine btw) which I thought was helpful for him too...

Good answer but the fact that you use a different import file as example all the times makes it cumbersome to read.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 554

There are many ways to import a python file, all with their pros and c...

Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.

I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:

def what_does_the_fox_say():
  print("vixens cry")

Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>

You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.

Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:

def moobar():
  print("hi")

Put this in /home/el/foo2/main.py:

execfile("/home/el/foo2/mylib.py")
moobar()

run the file:

el@apollo:/home/el/foo$ python main.py
hi

The function moobar was imported from mylib.py and made available in main.py

Put this in /home/el/foo3/chekov.py:

def question():
  print "where are the nuclear wessels?"

Put this in /home/el/foo3/main.py:

from chekov import question
question()
el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?

If you defined other functions in chekov.py, they would not be available unless you import *

Example 4, Import riaa.py if it's in a different file location from where it is imported

  • Put this in /home/el/foo4/stuff/riaa.py: def watchout(): print "computers are transforming into a noose and a yoke for humans"
  • Put this in /home/el/foo4/main.py: import sys import os sys.path.append(os.path.abspath("/home/el/foo4/stuff")) from riaa import * watchout()
el@apollo:/home/el/foo4$ python main.py 
computers are transforming into a noose and a yoke for humans

That imports everything in the foreign file from a different directory.

os.system("python yourfile.py")
import os
os.system("python yourfile.py")

Example 6, import your file via piggybacking the python startuphook:

~/.pythonrc.py
class secretclass:
    def secretmessage(cls, myarg):
        return myarg + " is if.. up in the sky, the sky"
    secretmessage = classmethod( secretmessage )

    def skycake(cls):
        return "cookie and sky pie people can't go up and "
    skycake = classmethod( skycake )
import user
msg = "The only way skycake tates good" 
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
$ python main.py
The only way skycake tates good is if.. up in the sky, 
the skycookie and sky pie people can't go up and  have the sky pie! 
SKYCAKE!

Example 7, Most Robust: Import files in python with the bare import command:

/home/el/foo5/
/home/el/foo5/herp
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

Make a new directory /home/el/foo5/herp/derp

__init__.py
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py

Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:

def skycake():
  print "SkyCake evolves to stay just beyond the cognitive reach of " +
  "the bulk of men. SKYCAKE!!"

The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;

from herp.derp.yolo import skycake
skycake()
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!

The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.

If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

You should also add Example 6: using __import__(py_file_name). Amazing guide anyway

What's the big difference between all of these, and why is one better than any other? For example 5, you write "Import files in python with the bare import command," but you also use the (bare?) import command in examples 1, 3 and 4, don't you?

Hey Eric! I think you got me wrong.. I just wanted to answer HelloGoodbyes question "what's the big difference between all of these" because I too was curious and found the blog entry (which is NOT mine btw) which I thought was helpful for him too...

Good answer but the fact that you use a different import file as example all the times makes it cumbersome to read.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 554

There are many ways to import a python file, all with their pros and c...

Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.

I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:

def what_does_the_fox_say():
  print("vixens cry")

Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>

You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.

Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:

def moobar():
  print("hi")

Put this in /home/el/foo2/main.py:

execfile("/home/el/foo2/mylib.py")
moobar()

run the file:

el@apollo:/home/el/foo$ python main.py
hi

The function moobar was imported from mylib.py and made available in main.py

Put this in /home/el/foo3/chekov.py:

def question():
  print "where are the nuclear wessels?"

Put this in /home/el/foo3/main.py:

from chekov import question
question()
el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?

If you defined other functions in chekov.py, they would not be available unless you import *

Example 4, Import riaa.py if it's in a different file location from where it is imported

  • Put this in /home/el/foo4/stuff/riaa.py: def watchout(): print "computers are transforming into a noose and a yoke for humans"
  • Put this in /home/el/foo4/main.py: import sys import os sys.path.append(os.path.abspath("/home/el/foo4/stuff")) from riaa import * watchout()
el@apollo:/home/el/foo4$ python main.py 
computers are transforming into a noose and a yoke for humans

That imports everything in the foreign file from a different directory.

os.system("python yourfile.py")
import os
os.system("python yourfile.py")

Example 6, import your file via piggybacking the python startuphook:

~/.pythonrc.py
class secretclass:
    def secretmessage(cls, myarg):
        return myarg + " is if.. up in the sky, the sky"
    secretmessage = classmethod( secretmessage )

    def skycake(cls):
        return "cookie and sky pie people can't go up and "
    skycake = classmethod( skycake )
import user
msg = "The only way skycake tates good" 
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
$ python main.py
The only way skycake tates good is if.. up in the sky, 
the skycookie and sky pie people can't go up and  have the sky pie! 
SKYCAKE!

Example 7, Most Robust: Import files in python with the bare import command:

/home/el/foo5/
/home/el/foo5/herp
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

Make a new directory /home/el/foo5/herp/derp

__init__.py
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py

Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:

def skycake():
  print "SkyCake evolves to stay just beyond the cognitive reach of " +
  "the bulk of men. SKYCAKE!!"

The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;

from herp.derp.yolo import skycake
skycake()
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!

The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.

If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

You should also add Example 6: using __import__(py_file_name). Amazing guide anyway

What's the big difference between all of these, and why is one better than any other? For example 5, you write "Import files in python with the bare import command," but you also use the (bare?) import command in examples 1, 3 and 4, don't you?

Hey Eric! I think you got me wrong.. I just wanted to answer HelloGoodbyes question "what's the big difference between all of these" because I too was curious and found the blog entry (which is NOT mine btw) which I thought was helpful for him too...

Good answer but the fact that you use a different import file as example all the times makes it cumbersome to read.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 4

There are many ways to import a python file:

Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.

I start out explaining the the easiest console example #1, then move toward the most professional and robust program example #5

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:

def what_does_the_fox_say():
  print "vixens cry"

Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>

You invoked the python function what_does_the_fox_say() from within the file fox through the python interpreter.

Option 2, Use execfile in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:

def moobar():
  print "hi"

Put this in /home/el/foo2/main.py:

execfile("/home/el/foo2/mylib.py")
moobar()

run the file:

el@apollo:/home/el/foo$ python main.py
hi

The function moobar was imported from mylib.py and made available in main.py

Put this in /home/el/foo3/chekov.py:

def question():
  print "where are the nuclear wessels?"

Put this in /home/el/foo3/main.py:

from chekov import question
question()
el@apollo:/home/el/foo3$ python main.py 
where are the nuclear wessels?

If you defined other functions in chekov.py, they would not be available unless you import *

Option 4, Import riaa.py if it's in a different file location from where it is imported

  • Put this in /home/el/foo4/bittorrent/riaa.py: def watchout_for_riaa_mpaa(): print "there are honeypot kesha songs on bittorrent that log IP " + "addresses of seeders and leechers. Then comcast records strikes against " + "that user and thus, the free internet was transmogified into " + "a pay-per-view cable-tv enslavement device back in the 20th century."
  • Put this in /home/el/foo4/main.py: import sys import os sys.path.append(os.path.abspath("/home/el/foo4/bittorrent")) from riaa import * watchout_for_riaa_mpaa()
el@apollo:/home/el/foo4$ python main.py 
there are honeypot kesha songs on bittorrent...

That imports everything in the foreign file from a different directory.

Option 5, Import files in python with the bare import command:

/home/el/foo5/
/home/el/foo5/herp
el@apollo:/home/el/foo5/herp$ touch __init__.py
el@apollo:/home/el/foo5/herp$ ls
__init__.py

Make a new directory /home/el/foo5/herp/derp

__init__.py
el@apollo:/home/el/foo5/herp/derp$ touch __init__.py
el@apollo:/home/el/foo5/herp/derp$ ls
__init__.py

Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:

def skycake():
  print "SkyCake evolves to stay just beyond the cognitive reach of " +
  "the bulk of men. SKYCAKE!!"

The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;

from herp.derp.yolo import skycake
skycake()
el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk 
of men. SKYCAKE!!

The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.

If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

Bonus protip, whether you are using Mac, Linux or Windows, you need to be using python's idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

Importing files in Python? - Stack Overflow

python file import
Rectangle 27 19

You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type

from root.parent.folder.file import variable, class, whatever

What if I want a relative path?

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 19

You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type

from root.parent.folder.file import variable, class, whatever

What if I want a relative path?

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 19

You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type

from root.parent.folder.file import variable, class, whatever

What if I want a relative path?

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 15

You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".

configfile = '~/config.py'

import os
import sys

sys.path.append(os.path.dirname(os.path.expanduser(configfile)))

import config

python - How to import a module given the full path? - Stack Overflow

python configuration python-import python-module
Rectangle 27 19

You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type

from root.parent.folder.file import variable, class, whatever

What if I want a relative path?

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 15

You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".

configfile = '~/config.py'

import os
import sys

sys.path.append(os.path.dirname(os.path.expanduser(configfile)))

import config

python - How to import a module given the full path? - Stack Overflow

python configuration python-import python-module
Rectangle 27 15

You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".

configfile = '~/config.py'

import os
import sys

sys.path.append(os.path.dirname(os.path.expanduser(configfile)))

import config

python - How to import a module given the full path? - Stack Overflow

python configuration python-import python-module
Rectangle 27 15

You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".

configfile = '~/config.py'

import os
import sys

sys.path.append(os.path.dirname(os.path.expanduser(configfile)))

import config

python - How to import a module given the full path? - Stack Overflow

python configuration python-import python-module
Rectangle 27 4

Case 1: you want to open the file in Python

from pyPdf import PdfFileReader, PageObject

pdf_toread = PdfFileReader(path_to_your_pdf)

# 1 is the number of the page
page_one = pdf_toread.getPage(1)

# This will dump the content (unicode string)
# According to the doc, the formatting is dependent on the
# structure of the document
print page_one.extractText()

As for the section, you can have a look to this answer

From this Acrobat help document, you can pass this to a subprocess:

import subprocess
import os

path_to_pdf = os.path.abspath('C:\test_file.pdf')
# I am testing this on my Windows Install machine
path_to_acrobat = os.path.abspath('C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe') 

# this will open your document on page 12
process = subprocess.Popen([path_to_acrobat, '/A', 'page=12', path_to_pdf], shell=False, stdout=subprocess.PIPE)
process.wait()

Just a suggestion: if you want to open the file at a specific section, you could use the parameter search=wordList where wordlist is a list of words seperated by spaces. The document will be opened and the search will be performed, the first result of it being highlighted. This way, as a wordlist, you can try to put the name of the section.

It is not necessary to open and read the PDF in Python. I would like to open the PDF with the default PDF reader and go to a specific page.

I am having a problem with this. My acrobat reader opens up and says "there was an error in opening the document. the filename directory name or volume label syntax is incorrect". I am unable to find a solution to this or even identify the error.

Python - open pdf file to specific page/section - Stack Overflow

python pdf
Rectangle 27 255

importlib
__import__
moduleName = input('Enter module name:')
import_module(moduleName)

Just import file without the '.py' extension.

You can mark a folder as a package, by adding an empty file named __init__.py.

You can use the __import__ function. It takes the module name as a string. (Again: module name without the '.py' extension.)

pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))

If you add an import filename to the init.py then you can import the module directly as the folder name.

help(__import__)
Because this function is meant for use by the Python     interpreter and not for general use it is better to use     importlib.import_module() to programmatically import a module.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 253

importlib
__import__
moduleName = input('Enter module name:')
import_module(moduleName)

Just import file without the '.py' extension.

You can mark a folder as a package, by adding an empty file named __init__.py.

You can use the __import__ function. It takes the module name as a string. (Again: module name without the '.py' extension.)

pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))

If you add an import filename to the init.py then you can import the module directly as the folder name.

help(__import__)
Because this function is meant for use by the Python     interpreter and not for general use it is better to use     importlib.import_module() to programmatically import a module.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 253

importlib
__import__
moduleName = input('Enter module name:')
import_module(moduleName)

Just import file without the '.py' extension.

You can mark a folder as a package, by adding an empty file named __init__.py.

You can use the __import__ function. It takes the module name as a string. (Again: module name without the '.py' extension.)

pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))

If you add an import filename to the init.py then you can import the module directly as the folder name.

help(__import__)
Because this function is meant for use by the Python     interpreter and not for general use it is better to use     importlib.import_module() to programmatically import a module.

Python: How to import other Python files - Stack Overflow

python python-import
Rectangle 27 30

For either the os.rename or shutil.move you will need to import the module. No * character is necessary to get all the files moved.

We have a folder at /opt/awesome called source with one file named awesome.txt.

in /opt/awesome
  ls
source
  ls source
awesome.txt

python 
>>> source = '/opt/awesome/source'
>>> destination = '/opt/awesome/destination'
>>> import os
>>> os.rename(source, destination)
>>> os.listdir('/opt/awesome')
['destination']

We used os.listdir to see that the folder name in fact changed. Here's the shutil moving the destination back to source.

>>> import shutil
>>> shutil.move(destination, source)
>>> os.listdir('/opt/awesome/source')
['awesome.txt']

This time I checked inside the source folder to be sure the awesome.txt file I created exists. It is there :)

Now we have moved a folder and its files from a source to a destination and back again.

docs.python.org/2/library/shutil.html This documentation shows that you have you your parameters switched for the shutil.move method.

I used the destination and source reversed to see that the files moved from the source and then back to it.... I could see how that is unclear.

How to move a file in Python - Stack Overflow

python file
Rectangle 27 1

Python uses a period(.) operator to refer to the directories/modules starting from the current folder and moving upwards in the directory tree with each period.For example, if you wish to import a file from python_file2.py and the file you are importing is contained in folder2 itself. This can be achieved by-from . import <filename>If you wish to import a file from python_file2.py and the file rests in folder1, you can add another period(.) to move one directory up and the statement will look like-from ..folder1 import <filename>filename is python_file1.py in your case. Hope this helps.

django - How to import a function from another directory file in pytho...

python django python-2.7