webdevRefinery Forum: Python: Import Help - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline ShanePerreault 

  • Sleep Burns My Eyes
  • Group: Members
  • Posts: 1075
  • Joined: 19-March 10
  • LocationChicago, USA
  • Expertise:PHP,Java,Javascript,Ruby on Rails,SQL

Posted 05 June 2011 - 04:04 PM (#1)

Python: Import Help


My file structure looks like this, this isn't a real app, this is just for learning purposes. I have a class file
person.py
which contains a Person class. I want to use in my script file
script.py
that class. The two files are in the same folder. I just am executing the script.py in terminal. How would I link that Person class in person.py to my script.py file to use the Person class.

Languages: PHP | JS | Ruby | Rails | C# | Python | Java
0


User is offline Cyril 

  • Group: Members
  • Posts: 2544
  • Joined: 03-August 10
  • Expertise:HTML,CSS,PHP,Javascript,Graphics

Posted 05 June 2011 - 04:06 PM (#2)

import person

website :: github :: twitter :: dribbble :: forrst
html, css, php, javascript, graphics
0


User is offline ShanePerreault 

  • Sleep Burns My Eyes
  • Group: Members
  • Posts: 1075
  • Joined: 19-March 10
  • LocationChicago, USA
  • Expertise:PHP,Java,Javascript,Ruby on Rails,SQL

Posted 05 June 2011 - 04:08 PM (#3)

cyrilmengin, on 05 June 2011 - 04:06 PM, said:

import person


Traceback (most recent call last):
  File "script.py", line 3, in <module>
    shane = Person('Shane Perreault', 'shaneprrlt@gmail.com')
NameError: name 'Person' is not defined


Languages: PHP | JS | Ruby | Rails | C# | Python | Java
0


User is offline arronhunt 

  • I'm a httpster
  • Group: Moderators
  • Posts: 3398
  • Joined: 09-March 10
  • LocationLos Angeles, CA
  • Expertise:HTML,CSS,Javascript,Graphics,Flash

Posted 05 June 2011 - 04:09 PM (#4)

You will need to import the other file as a module like this:
import person

DO NOT OPEN THIS

Spoiler
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 05 June 2011 - 04:13 PM (#5)

Alternatively,
from person import *
so you don't have to do "person.whatever" and can just type "whatever"
reputation += 1 if post.is_helpful else 0
0


User is offline ShanePerreault 

  • Sleep Burns My Eyes
  • Group: Members
  • Posts: 1075
  • Joined: 19-March 10
  • LocationChicago, USA
  • Expertise:PHP,Java,Javascript,Ruby on Rails,SQL

Posted 05 June 2011 - 04:15 PM (#6)

ianonavy, on 05 June 2011 - 04:13 PM, said:

Alternatively,
from person import *
so you don't have to do "person.whatever" and can just type "whatever"


Thank you, this worked perfectly.

Languages: PHP | JS | Ruby | Rails | C# | Python | Java
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 05 June 2011 - 06:08 PM (#7)

Actually, after doing a bit of research,

Quote

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

from the Python Modules page

Basically, what it's saying that it is recommended you do:
import person # everything, even private methods beginning with "__" are imported
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com') # should work

Also note that when writing Python modules, it isn't necessary to put each class within its own file. For example, if you want to encapsulate a person, you could write people.py and add all of the classes and methods related to people. Generally, as a rule, a module represents a single reusable unit.
reputation += 1 if post.is_helpful else 0
1


User is offline Sephern 

  • Group: Moderators
  • Posts: 967
  • Joined: 04-June 10
  • LocationReading, UK
  • Expertise:HTML,CSS,PHP,Javascript,Python

Posted 06 June 2011 - 02:05 AM (#8)

ianonavy, on 05 June 2011 - 06:08 PM, said:

Actually, after doing a bit of research,

from the Python Modules page

Basically, what it's saying that it is recommended you do:
import person # everything, even private methods beginning with "__" are imported
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com') # should work

Also note that when writing Python modules, it isn't necessary to put each class within its own file. For example, if you want to encapsulate a person, you could write people.py and add all of the classes and methods related to people. Generally, as a rule, a module represents a single reusable unit.

Its also worth noting that you can use the 'from' syntax to only import certain classes.
from random import randint

randint(1, 10)

would be the same as
import random

random.randint(1, 10)


The one that you'll be using most often is the top one. The reason for this is namespacing - the modules system means that you can have functions defined under the same name, using different classes. For example, the Person module could have a function called rename, as could a completely different module. Different modules can also have classes which have the same names and stuff too. It means that if you're making your code distributable, you know that other functions aren't going to overload yours, and vice versa (via naming conflicts). This is why it's generally frowned upon to import everything.

The use case for importing specific parts of modules you need into the global namespace (which means you don't have to put the module name beforehand) is that you can use it as shorthand throughout your code, which is good providing that you can be reasonably sure that naming conflicts won't occur. For example, it's unlikely that you, or anybody else, would define a function called randint in the global namespace so it would probably be okay to import it directly from the random module.
0


User is offline ShanePerreault 

  • Sleep Burns My Eyes
  • Group: Members
  • Posts: 1075
  • Joined: 19-March 10
  • LocationChicago, USA
  • Expertise:PHP,Java,Javascript,Ruby on Rails,SQL

Posted 06 June 2011 - 05:49 AM (#9)

Oh, I was making a syntactical mistake. When I was using
import person
, I wasn't doing this when creating objects:
shane = person.Person()
. That was the mistake, fixed, and working great. Yeah, I was thinking the same thing after reading that
from person import *
was bad.

Also, in general. Modules are almost files with sets of related classes and functions right. So instead of having a folder with all related class files like in PHP, I have a file with all related classes and methods?

Furthermore, what if I have a module in a folder? How would I import it. What if my file structure was:

/main
+script.py
+/classes
++person.py


And I wanted to use person.py in my script.py. How would I import that?

Languages: PHP | JS | Ruby | Rails | C# | Python | Java
0


User is offline ianonavy 

  • Group: Members
  • Posts: 685
  • Joined: 14-April 10
  • Expertise:HTML,CSS,Java,Javascript,Python

Posted 06 June 2011 - 12:53 PM (#10)

ShanePerreault, on 06 June 2011 - 05:49 AM, said:

Also, in general. Modules are almost files with sets of related classes and functions right. So instead of having a folder with all related class files like in PHP, I have a file with all related classes and methods?

Not almost, that's exactly what they are for. (:

ShanePerreault, on 06 June 2011 - 05:49 AM, said:

Furthermore, what if I have a module in a folder? How would I import it.

I know it sounds odd, but you're going to need an empty file named "__init__.py" in your /main/classes directory (as well as any subdirectory that you want to import classes from. Probably good to have one in /main too to make your entire folder an importable module). Once your directory structure looks like this:

main
|-- classes
| |-- __init__.py
| `-- person.py
|-- __init__.py
`-- main.py


you can treat the subdirectories as modules themselves and do any of the following below:
# This is if you hate yourself and want to type out the full path name every time.
import classes.person
shane = classes.person.Person('Shane Perreault', 'shaneprrlt@gmail.com')

or if that's awkward for you:
# This is okay unless you have multiple modules with the same name in different packages.
from classes import person
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com')

and if you're even lazier:
# Only use this if you are certain that you won't have any conflicting "Person" classes.
from class.person import Person
shane = Person('Shane Perreault', 'shaneprrlt@gmail.com')

You could even do:
# This is pretty much the same as example 2, but slightly more verbose and less Pythonic.
import classes.person
person = classes.person
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com')

If that wasn't clear enough, read this.

Edit:
Spoiler

reputation += 1 if post.is_helpful else 0
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services