[Tutor] PYTHONPATH (Mac OS X)

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] PYTHONPATH (Mac OS X)

Stayvoid
Hey there!

How to set it right?


Cheers!
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Bob Gailer
On 12/18/2011 5:45 PM, Stayvoid wrote:
> Hey there!
>
> How to set it right?
You may not get an answer as your question is pretty vague.

Please clarify, or expand, or tell us what problem you are having or
trying to solve.

--
Bob Gailer
919-636-4239
Chapel Hill NC

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Stayvoid
> Please clarify, or expand, or tell us what problem you are having or
> trying to solve.

Hi!

I want to have a possibility to import modules from the folder, which
is not included in the load path.

Example:

module.py
---------
def testfunc(name):
  file = open(name)
  return len(file.readlines())

if __name__ == "__main__":
  print testfunc(module.py)

Code listing (shell):
python /Users/Username/pythonmodules/module.py

NameError: name 'module.py' is not defined

Kind regards.
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Dave Angel-3
On 12/19/2011 08:33 PM, Stayvoid wrote:

>> Please clarify, or expand, or tell us what problem you are having or
>> trying to solve.
> Hi!
>
> I want to have a possibility to import modules from the folder, which
> is not included in the load path.
>
> Example:
>
> module.py
> ---------
> def testfunc(name):
>    file = open(name)
>    return len(file.readlines())
>
> if __name__ == "__main__":
>    print testfunc(module.py)
>
> Code listing (shell):
> python /Users/Username/pythonmodules/module.py
>
> NameError: name 'module.py' is not defined
>
> Kind regards.
>
First rule:  include the complete error message, including the traceback.

Also, make sure you copy/paste the message, not paraphrase or retype it.

Anyway, chances are the error occurs because you didn't have any quotes
around the filename.  Python is forced to look up the name "module" in
the global dictionary, and it can't find it.  Of course, the error
message wouldn't be exactly that.


--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Alan Gauld
In reply to this post by Stayvoid
On 20/12/11 01:33, Stayvoid wrote:

> I want to have a possibility to import modules from the folder, which
> is not included in the load path.

The code and error below has nothing to do with importing modules or the
PYTHONPATH value.

> module.py
> ---------
> def testfunc(name):
>    file = open(name)
>    return len(file.readlines())
>
> if __name__ == "__main__":
>    print testfunc(module.py)

This is simply opening the file module.py as any other file.
It does not invoke Pythons import mechanism.

> Code listing (shell):
> python /Users/Username/pythonmodules/module.py
>
> NameError: name 'module.py' is not defined


Please post the full error text not a summary.
But in this case it isc complaining about the favct that you have not
put quotes around the filename so it thinks module.py is a variable, but
when it looks for it, it is not defined.

BTW To set PYTHONPATH in a MacOSX environment you have to edit your
.profile or .bash_profile (I can never remember which!) and add a line like

export PYTHONPATH=/path/to/your/modules:/another/module/path/here

HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Stayvoid
Hey there!

I'm reading Lutz's Learning Python.

Here is some code from the book.

There is a module called lcclient_lutz.py:

from lengthcounter_lutz import countLines, countChars
print countLines('lengthcounter_lutz.py'), countChars('lengthcounter_lutz.py')

And there is another one called lengthcounter_lutz.py:

def countLines(name):
    file = open(name)
    return len(file.readlines())

def countChars(name):
    return len(open(name).read())

def test(name):
    return "Lines:", countLines(name), "Chars:", countChars(name)

if __name__ == '__main__':
    print test('lengthcounter_lutz.py')

I've got an error while trying to load lcclient_lutz module:
python /Users/Username/Python_modules/lcclient_lutz.py
Traceback (most recent call last):
  File "/Users/Username/Python_modules/lcclient_lutz.py", line 2, in <module>
    print countLines('lengthcounter_lutz.py'),
countChars('lengthcounter_lutz.py')
  File "/Users/Username/Python_modules/lengthcounter_lutz.py", line 2,
in countLines
    file = open(name)
IOError: [Errno 2] No such file or directory: 'lengthcounter_lutz.py'

How to fix it? Is it connected with the PYTHONPATH variable?

P.S. There might be an error in the lengthcounter_lutz module, because
it makes mistakes while counting.


Kind regards.
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Alan Gauld
On 25/12/11 22:55, Stayvoid wrote:

> There is a module called lcclient_lutz.py:
>
> from lengthcounter_lutz import countLines, countChars
> print countLines('lengthcounter_lutz.py'), countChars('lengthcounter_lutz.py')
>
> countChars('lengthcounter_lutz.py')
>    File "/Users/Username/Python_modules/lengthcounter_lutz.py", line 2,
> in countLines
>      file = open(name)
> IOError: [Errno 2] No such file or directory: 'lengthcounter_lutz.py'

> How to fix it? Is it connected with the PYTHONPATH variable?

PYTHONPATH only helps Python find the files to import.
Since the error message is pointing at code insiude the imported
functions then clearly PYTHONPATH is working just fine.


So what is the error?
Simply that python is saying it cannot find the file.
So it is probably in a different folder to the one in which the program
is running. You need to provide a valid path to the file,

> P.S. There might be an error in the lengthcounter_lutz module, because
> it makes mistakes while counting.

Thats possible. Or it may e worlking to a different definition of
success to the one you expect! Counting words and letters is quite a
subjective past-time.

So what is it doing exactly that seems wrong?
What input? What output? What did you expect?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Stayvoid
>Simply that python is saying it cannot find the file.
>So it is probably in a different folder to the one in which the program is running. You need to provide a valid path to the file,

Those files are in the same folder:
/Users/Username/Python_modules/

I don't want to write a full path here:
if __name__ == '__main__':
   print test('lengthcounter_lutz.py')

How to add this directory to the search path?

>So what is it doing exactly that seems wrong?
>What input? What output? What did you expect?

I have a slightly different copy of this file:

def countLines(name):
    file = open(name.__file__)
    return len(file.readlines())

def countChars(name):
    return len(open(name.__file__).read())

def test(name):
    return "Lines:", countLines(name), "Chars:", countChars(name)

if __name__ == '__main__':
    import lengthcounter
    print test(lengthcounter)

I've tried to run it in the interactive shell:

import lengthcounter as lc
lc.test(lengthcounter)

And here is the output:

('Lines:', 5, 'Chars:', 885)

But that code has 13 lines and 317 characters according to the Emacs' counter.
Where is an error?

Cheers.
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Dave Angel-3
On 12/26/2011 07:23 AM, Stayvoid wrote:

>> Simply that python is saying it cannot find the file.
>> So it is probably in a different folder to the one in which the program is running. You need to provide a valid path to the file,
> Those files are in the same folder:
> /Users/Username/Python_modules/
>
> I don't want to write a full path here:
> if __name__ == '__main__':
>     print test('lengthcounter_lutz.py')
>
> How to add this directory to the search path?
Once again, there is no search path for the open() function.  You can
either supply an absolute name (eg. starting with leading slash), or you
can supply a path relative to the current directory.  If you're not sure
what the current directory is, you can fetch it with something like:

import os

print os.path.abspath(os.curdir)

But if you're running the script from the terminal window, it should
simply be the current directory for that window.  If you're running it
from some other shell, you'd have to consult that shell's docs.




--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Alan Gauld
In reply to this post by Stayvoid
On 26/12/11 12:23, Stayvoid wrote:
>> Simply that python is saying it cannot find the file.
>> So it is probably in a different folder ...
> Those files are in the same folder:
> /Users/Username/Python_modules/

And is that where you are running the code from? That is the critical
factor.

By just providing the filename Python assumes its in the same folder
that you ran the program from. If you run it from somewhere else you
*must* provide the full (or relative() path to the file. There is no
option for Python to search for it.

> I don't want to write a full path here:
> if __name__ == '__main__':
>     print test('lengthcounter_lutz.py')

You don't have any option.
You either type in the full path or you get the user to tell you
in some way - either with a prompt or via an input argument.


> I have a slightly different copy of this file:
>
> def countLines(name):
>      file = open(name.__file__)
>      return len(file.readlines())
>
> def countChars(name):
>      return len(open(name.__file__).read())
>
> def test(name):
>      return "Lines:", countLines(name), "Chars:", countChars(name)
>
> if __name__ == '__main__':
>      import lengthcounter
>      print test(lengthcounter)
>
> I've tried to run it in the interactive shell:
>
> import lengthcounter as lc
> lc.test(lengthcounter)

Are you sure it is your version that's being picked up?
Try renaming it to something guaranteed to be unique. See if the results
are the same.

Also I would expect the prompt to complain because you are passing
lengthcounter as a name to the test function, but you imported
lengthcounter as lc, so python should not know about lengthcounter
unless you had already imported it (or another file?)
eg.

 >>> import random as r
 >>> random.__file__
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined
 >>>

Try starting a fresh interpreter session and repeating the test.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Stayvoid
>You don't have any option.
>You either type in the full path or you get the user to tell you
>in some way - either with a prompt or via an input argument.

OK, now I get it.

How can I tell this to the end-user?
Should I write a README file or what?

I've tried to run lengthcounter_lutz from the file's folder and this worked out.
cd /Users/Username/Python_modules/
python /Users/Username/Python_modules/lengthcounter_lutz.py
('Lines:', 12, 'Chars:', 285)


But I can't understand what's wrong with the second one:
def countLines(name):
    file = open(name.__file__)
    return len(file.readlines())

def countChars(name):
    return len(open(name.__file__).read())

def test(name):
    return "Lines:", countLines(name), "Chars:", countChars(name)

if __name__ == '__main__':
    import lengthcounter
    print test(lengthcounter)

>>> import lengthcounter
>>> lengthcounter.test(lengthcounter)
('Lines:', 5, 'Chars:', 885)


That PYTHONPATH variable has no connection with the mess above. When
should I use it?


Thanks for your help.
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

Alan Gauld
On 30/12/11 13:11, Stayvoid wrote:

> I've tried to run lengthcounter_lutz from the file's folder and this worked out.
> cd /Users/Username/Python_modules/
> python /Users/Username/Python_modules/lengthcounter_lutz.py


You should only have needed:

 > python ./lengthcounter_lutz.py

the ./ tells Linux to use the current folder as startuing point.

> ('Lines:', 12, 'Chars:', 285)
>
>
> But I can't understand what's wrong with the second one:
> def countLines(name):
>      file = open(name.__file__)
>      return len(file.readlines())
>
> def countChars(name):
>      return len(open(name.__file__).read())
>
> def test(name):
>      return "Lines:", countLines(name), "Chars:", countChars(name)
>
> if __name__ == '__main__':
>      import lengthcounter
>      print test(lengthcounter)
>
>>>> import lengthcounter
>>>> lengthcounter.test(lengthcounter)
> ('Lines:', 5, 'Chars:', 885)

Neither do I but you can debug it easily from the >>> prompt by calling
the individual lines/functions. Try:

 >>> import lengthcounter as lc
 >>> print lc.__file__   # to check it's using the expected file
 >>> # assuming it is...
 >>> print (open(lc.__name__),read())   # check the contents is what we
expect
 >>> print ( len(open(lc.__name__),read()) )   # check the length result
 >>> print ( len(open(lc.__name__),readlines()) )   # check the lines

> That PYTHONPATH variable has no connection with the mess above. When
> should I use it?

Python uses PYTHONPATH to find modules when you import them.

So when we do import lengthcounter above we dont need to be in the same
folder as lengthcounter.py to be able to import it.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] PYTHONPATH (Mac OS X)

David Rock
In reply to this post by Stayvoid
* Stayvoid <[hidden email]> [2011-12-30 16:11]:

> >You don't have any option.
> >You either type in the full path or you get the user to tell you
> >in some way - either with a prompt or via an input argument.
>
> OK, now I get it.
>
> How can I tell this to the end-user?
> Should I write a README file or what?
>
> I've tried to run lengthcounter_lutz from the file's folder and this worked out.
> cd /Users/Username/Python_modules/
> python /Users/Username/Python_modules/lengthcounter_lutz.py
> ('Lines:', 12, 'Chars:', 285)
>
>
> But I can't understand what's wrong with the second one:
> def countLines(name):
>     file = open(name.__file__)
>     return len(file.readlines())
>
> def countChars(name):
>     return len(open(name.__file__).read())
>
> def test(name):
>     return "Lines:", countLines(name), "Chars:", countChars(name)
>
> if __name__ == '__main__':
>     import lengthcounter
>     print test(lengthcounter)
>
> >>> import lengthcounter
> >>> lengthcounter.test(lengthcounter)
> ('Lines:', 5, 'Chars:', 885)
>
Looking at page 1119 in the learning Python book, I might venture a
guess as to where the difference lies.  You are calling test as:
test(lengthcounter), but that is not the name of a file (the name of a
file should be in quotes). lengthcounter in this case is a variable,
not a filename.  The behavior in this case is probably undetermined.

I suggest doing a manual check on the file named lengthcounter.pyc, and
I'll bet you will find something closer to 5 lines and 885 characters.
pyc files are the bytecode version of your file that gets generated
automatically and is the code that is actually executed.

Somehow, calling the method test inside the interpreter is different
from running it on the commandline and you are picking up the pyc file.  
In either case, this format is iffy at best:

if __name__ == '__main__':
    import lengthcounter
    print test(lengthcounter)

should really be:

if __name__ == '__main__':
    import lengthcounter
    print lengthcounter.test(lengthcounter)

to avoid ambiguous behavior.

--
David Rock
[hidden email]

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

attachment0 (197 bytes) Download Attachment