Need basic help - Orienting myself in Python

This is the place for queries that don't fit in any of the other categories.

Need basic help - Orienting myself in Python

Postby beginnersluke » Mon Nov 25, 2013 11:11 pm

Hello all,

Thanks for reading this. I need some very basic help in Python (2.7.6) I am basically a novice programming (aside from writing programs to display "hi mom" in the late 80s in BASIC, I know nothing). I am trying to run some python scripts that someone has already written.

However, I'm so clueless (hey, at least I admit to my ignorance) I don't really know how to do this.

I've installed Python on my Win7 computer. I can run the shell and the fancier IDLE GUI shell.

The script I am trying to run requires a module called BeautifulSoup.

I have tried installing this in several ways, but it doesn't seem to be working. When my script tries to call it up, it says it can't find it: "ImportError: No module named BeautifulSoup"

The main thing I don't understand is sort of "where I am" when I am in the python shell. In other words, what folder am I in and what folder to I need to put the BeautifulSoup files into. (Right now, it's a folder in my c:\Python27 directory.)

The BeautifulSoup page mentions installing it using PIP, so I downloaded that and ran the install. This seemed to work, the shell told me it restarted, but when I tried to run "pip install BeautifulSoup" it highlighted "install" and gave an invalid syntax error.

So basically, I am lost and I am lost enough that I don't even know the right question to ask. Feel free to make fun of me for this , but any help would also be greatly appreciated.

For the larger context, what I am trying to do it build an Arduino controlled musical holiday card, as described here: http://jarv.org/20130407-jarv-wp-static ... p=676.html

I've got my music in an xml file and am trying to run the 'xml2h.py' script.

Again, I am not sure where to properly place the files that go with that script and the script itself. (Though I do seem to be able to run it.) I think one thing that would help me orient myself would just be the knowledge of where to put it so I could "run" the script from the main shell command line, or how to move around between directories in the shell.

Thank you so much for any help, and I'll be glad to answer any questions, since I may have forgotten to put in some pretty important information.

Luke
Last edited by micseydel on Mon Nov 25, 2013 11:50 pm, edited 1 time in total.
Reason: Locked OP.
beginnersluke
 
Posts: 5
Joined: Mon Nov 25, 2013 10:48 pm

Re: Need basic help - Orienting myself in Python

Postby micseydel » Tue Nov 26, 2013 12:00 am

beginnersluke wrote:The BeautifulSoup page mentions installing it using PIP, so I downloaded that and ran the install. This seemed to work, the shell told me it restarted, but when I tried to run "pip install BeautifulSoup" it highlighted "install" and gave an invalid syntax error.

That command is supposed to be run in a Windows command prompt/shell, not the Python shell. The windows shell is something like
Code: Select all
C:\>

whereas Python looks like
Code: Select all
>>>

Both of those accept different things, the first one no Python code and the second one only Python code. If you put your command into the first one it very well may solve your problem (or at least allow you to move forward).

beginnersluke wrote:The main thing I don't understand is sort of "where I am" when I am in the python shell. In other words, what folder am I in and what folder to I need to put the BeautifulSoup files into. (Right now, it's a folder in my c:\Python27 directory.)

If your'e in a Python shell, you can use
Code: Select all
os.getcwd()

to get your current path, assuming you've run
Code: Select all
import os

in that session already. Do note however that if you've install a module such as BeautifulSoup it shouldn't matter where you are. In general though if you care about where the shell is in your file system, if you're using Python through a Windows command prompt then the current working directory for Python is the directory you launched it in, and IIRC IDLE starts in your home directory.

I hope I've helped a bit, if you still have questions or if anything I said was unclear feel free to post any followup questions.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need basic help - Orienting myself in Python

Postby beginnersluke » Tue Nov 26, 2013 4:57 pm

Thank you micseydel. Based on some of your hints, I think I've made some progress and I definitely just have a better grasp as to what is going on when I'm typing. I have a long way to go, but I fell as I've learned something, so thanks again.

I think I've gotten the BeautifulSoup module loaded manually. That is, when I type "import BeautifulSoup" I don't get any errors, just a new command line, so I think it worked.

Now I am trying to run the xml2h script I've downloaded.

I am having a problem with the right syntax though.

So in the example (which I posted above), the user simply type the following at the python shell prompt: "xml2h.py /tmp/song.xml ~/prog/Playtune/songs/spng.h".

I'm not sure of the reason (different python version?) I can't just type a script name at the Python shell prompt, I've found (thanks google!) "execfile "script.py". (Substituting an actual script name.)

What I can't figure out now is the proper syntax for the input and output file. I've tried 5 different variations, but it doesn't like any of them.

For example, I type:
execfile ("xml2h.py" /python27/cards/songs/feliznav.xml /python27/cards/songs.feliznav.h)


But python returns:

Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
execfile ("xml2h.py" /python27/cards/songs/feliznav.xml /python27/cards/songs.feliznav.h)
NameError: name 'python27' is not defined


Any help will be greatly appreciated. (This is for a Christmas card my family is trying to build and I will absolutely mention this forum in the notes if we actually get it working.)

Thanks again,

Luke
beginnersluke
 
Posts: 5
Joined: Mon Nov 25, 2013 10:48 pm

Re: Need basic help - Orienting myself in Python

Postby stranac » Tue Nov 26, 2013 6:00 pm

execfile is definitely the wrong way to do it.

You should run the script from the windows shell.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 885
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need basic help - Orienting myself in Python

Postby beginnersluke » Tue Nov 26, 2013 6:08 pm

Okay, so let me ask a more basic question.

So now that I have installed python, python scripts can run from the Windows DOS prompt. Is that correct?

So how do I know what I should run from within Windows and what I should run from within Python?

Thanks,

Luke
beginnersluke
 
Posts: 5
Joined: Mon Nov 25, 2013 10:48 pm

Re: Need basic help - Orienting myself in Python

Postby micseydel » Tue Nov 26, 2013 6:14 pm

From the Windows DOS-like command prompt you can invoke the Python program and pass as a command line argument to that program a script name, which the Python program (interpreter) will then run. Further command line arguments are passed to the Python script by the Python interpreter.

The way to tell what should go where is that Python source code goes in a Python shell, and non-Python source code if it's a command will go in an operating system (e.g. Windows) command prompt. (I mention this because of Linux and OS X, since there is some variability among the three, especially for Windows, so if you see commands elsewhere they may not work.) If the command begins with "python " then you're probably trying to invoke the interpreter, and so it doesn't work in the Python shell since "python" isn't inherently valid Python source code.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need basic help - Orienting myself in Python

Postby beginnersluke » Tue Nov 26, 2013 7:18 pm

Okay, thanks so much. It's starting to make more sense.

Back to the first answer, you suggested running the pip install... command from the Windows shell. It returns that "'pip' is not a recognized command."

For background, I installed both setuptools and pip as detailed here (http://www.pip-installer.org/en/latest/installing.html) and that all seemed to go fine.

When I try to run the xml2h.py[/] script, I get an: "ImportError: No module names BeautifulSoup" error. This is why I think I need to run the [i]pip install BeautifulSoup command as mentioned on the BeautifulSoup page. (I hope that's the problem at least.)

Thanks so much,

Luke
beginnersluke
 
Posts: 5
Joined: Mon Nov 25, 2013 10:48 pm

Re: Need basic help - Orienting myself in Python

Postby Kebap » Tue Nov 26, 2013 7:22 pm

Hey old time BASIC companero! :mrgreen: It probably goes a little something like this:

Code: Select all
C:\python 27>python
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'hi Mom!'
hi Mom!
>>>
>>> with open("himom.py", "w") as myfile:
...   myfile.write("print 'Hi Mom!'")
...
>>> exit()

C:\python 27>
C:\python 27>dir /b himom.py
himom.py
C:\python 27>
C:\python 27>python himom.py
Hi Mom!

C:\python 27>




Basically, you have to watch out, whether you are looking into a problem within python or with you operating system (in this case win7). You will probably face some of the latter, as unfortunately Python is not shipped with Windows. On Linux, that is so. That is why many tutorials online may assume you use Linux. On the other hand, this is easy to fix, as soon as you know what to look for.

Another suggestion, add your python folder to your Windows' System Variable "Path". Then you can type "python" anywhere, and move your projects to their own folders, etc.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Need basic help - Orienting myself in Python

Postby micseydel » Tue Nov 26, 2013 7:25 pm

Did you run "python ez_setup.py" in a Windows shell and it succeeded? There might be administrator issues if it did seem to succeed. Also, you may need to manipulate your PATH variable (as mentioned above) for reasons that always baffle me (installers should absolutely be doing this for you, I'm very sorry they do not).
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need basic help - Orienting myself in Python

Postby stranac » Tue Nov 26, 2013 7:45 pm

For windows, you should just grab setuptools and pip installers from this site:
http://www.lfd.uci.edu/~gohlke/pythonlibs/

It's by far the easiest way of getting pip installed on windows.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 885
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need basic help - Orienting myself in Python

Postby beginnersluke » Tue Nov 26, 2013 8:25 pm

Yay!

I've made some pretty good progress. In fact, I think it's worked! (I'll of course have to move on to the next step and get this on my Arduino chip to really know, but I did get a .h file, which is what I needed.

The PIP install did work properly, but it was as you suggested a path issue. I added a path for \python27\pip and all the other pip directories I saw, but pip.exe actually gets put in the \python27\scripts directory. I added a path entry to that directory and the BeautifulSoup install with [i]pip[/] went as expected.

After that I ran the script, from the Windows shell as suggested and it worked perfectly.

Thanks so much for all the help. I truly appreciate it.

Is there a good basic tutorial to python somewhere on youtube? I looked for one, but nothing I found seemed that great. It would be nice to know more about all of this stuff.

Thanks again!
beginnersluke
 
Posts: 5
Joined: Mon Nov 25, 2013 10:48 pm

Re: Need basic help - Orienting myself in Python

Postby micseydel » Tue Nov 26, 2013 9:15 pm

I don't personally know of video tutorials but you can try looking here. If you find any of them bad you're welcome to comment about it and we can remove/replace it.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need basic help - Orienting myself in Python

Postby dr3311 » Mon Dec 02, 2013 9:26 pm

The best tutorial i have found is "How to Think Like a Computer Scientist" pyhthon 2 version. I have been getting into python for like a month now. For 25 days i learned virtually nothing. Then i found the above book/tutorial, I learned more from that in a few hours than i did from other sources all month. Also, the man who wrote the book is great. If you have any questions, or even sugestions, you can just email him. He usually gets back to me within two days.
dr3311
 
Posts: 5
Joined: Mon Dec 02, 2013 8:20 pm

Re: Need basic help - Orienting myself in Python

Postby micseydel » Mon Dec 02, 2013 9:30 pm

Yeah, "How to think like a computer scientist" is my favorite written tutorial although the question here was about a video tutorial. (By the way, that tutorial is my favorite, but I consider the title to be quite misleading!)
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need basic help - Orienting myself in Python

Postby dr3311 » Mon Dec 02, 2013 9:32 pm

Yes, the title is stupid. Actually I almost didnt use this tutorial because of it. Man am i glad i did. I have looked at alot of them, interactive tuts, video tuts, all of them. And httlacs is by far the best.
dr3311
 
Posts: 5
Joined: Mon Dec 02, 2013 8:20 pm


Return to General Coding Help

Who is online

Users browsing this forum: Cereal and 2 guests