Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45410 > unrolled thread
| Started by | Charles Smith <cts.private.yahoo@gmail.com> |
|---|---|
| First post | 2013-05-16 00:48 -0700 |
| Last post | 2013-05-16 02:23 -0700 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
executing python scripts that are symlinked Charles Smith <cts.private.yahoo@gmail.com> - 2013-05-16 00:48 -0700
Re: executing python scripts that are symlinked Andrew Berg <bahamutzero8825@gmail.com> - 2013-05-16 03:12 -0500
Re: executing python scripts that are symlinked Dave Angel <davea@davea.name> - 2013-05-16 04:18 -0400
Re: executing python scripts that are symlinked Charles Smith <cts.private.yahoo@gmail.com> - 2013-05-16 01:29 -0700
Re: executing python scripts that are symlinked Dave Angel <davea@davea.name> - 2013-05-16 10:45 -0400
Re: executing python scripts that are symlinked Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-16 09:04 +0000
Re: executing python scripts that are symlinked Charles Smith <cts.private.yahoo@gmail.com> - 2013-05-16 02:23 -0700
| From | Charles Smith <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2013-05-16 00:48 -0700 |
| Subject | executing python scripts that are symlinked |
| Message-ID | <95bc1155-0a83-4ab1-8b76-68a819b2e29a@en2g2000vbb.googlegroups.com> |
Hi. How can I say, from the cmd line, that python should take my CWD as my CWD, and not the directory where the script actually is? I have a python script that works fine when it sits in directory WC, but if I move it out of WC to H and put a symlink from H/script to WC, it doesn't find the packages that are in WC. Also, if I use the absolute path to H, it won't find them, but I guess I can understand that. Someone said on the net that python doesn't know whether a file is real or a symlink, but I think that somehow, python is able to find out where the real file is and treat that as its base of operations. Charles. ---------------------------------------------------------- http://www.creative-telcom-solutions.de
[toc] | [next] | [standalone]
| From | Andrew Berg <bahamutzero8825@gmail.com> |
|---|---|
| Date | 2013-05-16 03:12 -0500 |
| Message-ID | <mailman.1740.1368691981.3114.python-list@python.org> |
| In reply to | #45410 |
On 2013.05.16 02:48, Charles Smith wrote: > Hi. > > How can I say, from the cmd line, that python should take my CWD as my > CWD, and not the directory where the script actually is? > > > I have a python script that works fine when it sits in directory WC, > but if I move it out of WC to H and put a symlink from H/script to WC, > it doesn't find the packages that are in WC. Also, if I use the > absolute path to H, it won't find them, but I guess I can understand > that. Symlinks can find their targets, but targets have absolutely no way of knowing where symlinks to them are. It's one-way. It would work if the actual file were in WC and you created a symlink inside H. -- CPython 3.3.1 | Windows NT 6.2.9200 / FreeBSD 9.1
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-05-16 04:18 -0400 |
| Message-ID | <mailman.1741.1368692298.3114.python-list@python.org> |
| In reply to | #45410 |
On 05/16/2013 03:48 AM, Charles Smith wrote: > Hi. > > How can I say, from the cmd line, that python should take my CWD as my > CWD, and not the directory where the script actually is? > > > I have a python script that works fine when it sits in directory WC, > but if I move it out of WC to H and put a symlink from H/script to WC, > it doesn't find the packages that are in WC. Also, if I use the > absolute path to H, it won't find them, but I guess I can understand > that. > > Someone said on the net that python doesn't know whether a file is > real or a symlink, but I think that somehow, python is able to find > out where the real file is and treat that as its base of operations. > You'd really better specify your environment - exact OS and Python version. symlink and cwd usually imply a Unix-type system, but cmd is a Windows thing. Then give examples of what your cwd is, what string you're typing at the shell prompt, and what's happening. For example windows is good at changing cwd long before the executable gets control, in some situations. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Charles Smith <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2013-05-16 01:29 -0700 |
| Message-ID | <8640d5b0-5e6e-4a54-9584-6e7ffcdf1389@k3g2000vbn.googlegroups.com> |
| In reply to | #45413 |
On 16 Mai, 10:18, Dave Angel <da...@davea.name> wrote:
> On 05/16/2013 03:48 AM, Charles Smith wrote:
>
> > Hi.
>
> > How can I say, from the cmd line, that python should take my CWD as my
> > CWD, and not the directory where the script actually is?
>
> > I have a python script that works fine when it sits in directory WC,
> > but if I move it out of WC to H and put a symlink from H/script to WC,
> > it doesn't find the packages that are in WC. Also, if I use the
> > absolute path to H, it won't find them, but I guess I can understand
> > that.
>
> > Someone said on the net that python doesn't know whether a file is
> > real or a symlink, but I think that somehow, python is able to find
> > out where the real file is and treat that as its base of operations.
>
> You'd really better specify your environment - exact OS and Python
> version. symlink and cwd usually imply a Unix-type system, but cmd is a
> Windows thing.
>
> Then give examples of what your cwd is, what string you're typing at the
> shell prompt, and what's happening.
Well, I'm on a ubuntu platform, running subversion, but I can't commit
this debugging tool into the working copy where I'm using it, so I
maintain it in my home directory. The tool does use production
packages, though.
So, if I say,
$ python fapi-test.py
and fapi-test.py really is there, then it works, using the codec
production package. But if I use a symlink instead, it says
Traceback (most recent call last):
File "test2", line 1, in <module>
from codec.support import *
ImportError: No module named codec.support
Python tells me Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
----------------------------------------------------------
http://www.creative-telcom-solutions.de
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-05-16 10:45 -0400 |
| Message-ID | <mailman.1755.1368715536.3114.python-list@python.org> |
| In reply to | #45414 |
On 05/16/2013 04:29 AM, Charles Smith wrote:
> On 16 Mai, 10:18, Dave Angel <da...@davea.name> wrote:
>> On 05/16/2013 03:48 AM, Charles Smith wrote:
>>
>>> Hi.
>>
>>> How can I say, from the cmd line, that python should take my CWD as my
>>> CWD, and not the directory where the script actually is?
>>
>>> I have a python script that works fine when it sits in directory WC,
>>> but if I move it out of WC to H and put a symlink from H/script to WC,
>>> it doesn't find the packages that are in WC. Also, if I use the
>>> absolute path to H, it won't find them, but I guess I can understand
>>> that.
>>
>>> Someone said on the net that python doesn't know whether a file is
>>> real or a symlink, but I think that somehow, python is able to find
>>> out where the real file is and treat that as its base of operations.
>>
>> You'd really better specify your environment - exact OS and Python
>> version. symlink and cwd usually imply a Unix-type system, but cmd is a
>> Windows thing.
>>
>> Then give examples of what your cwd is, what string you're typing at the
>> shell prompt, and what's happening.
>
>
> Well, I'm on a ubuntu platform,
Good. Linux seldom gets in the way.
> running subversion, but I can't commit
> this debugging tool into the working copy where I'm using it, so I
> maintain it in my home directory. The tool does use production
> packages, though.
>
> So, if I say,
>
> $ python fapi-test.py
>
> and fapi-test.py really is there,
where? Be explicit. When copying the python invocation line above, you
never bothered to include the current directory that's generally
displayed right in front of that $ prompt.
> then it works, using the codec
> production package.
What codec production package. And where is it installed? And how do
you intend that Python find it?
But if I use a symlink instead,
So you delete the pfapi-tst.py out of the current directory, and create
instead a symlink in the current directory that points to its real
location And without changing current directory, you run the same
command and it runs till it gets to the import line, at which point it
gives a traceback that has nothing to do with the file you're running???
The traceback thinks you're running test2.py.
it says
>
> Traceback (most recent call last):
> File "test2", line 1, in <module>
> from codec.support import *
> ImportError: No module named codec.support
>
>
> Python tells me Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
>
>
Take a look at Stephen's example. He runs some things, shows you
exactly what he's running, and what happens. The things he runs may not
match what you did, but we cannot tell.
From within your script, you can display os.getcwd(), you can ...
import sys, os
print "cwd is", os.getcwd()
print "file is", __file__
print "PATH is", os.getenv("PATH")
print "python-path is", os.getenv("PYTHONPATH")
print "sys.path is", sys.path
import codec
print "codec file is", codec.__file__
if you run that both as a regular file and on a symlink pointing to that
file, comparing the two runs could give you a clue as to what's
different. My suspicion is that you have a different cwd when you test
it the second way.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-05-16 09:04 +0000 |
| Message-ID | <5194a102$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #45410 |
On Thu, 16 May 2013 00:48:45 -0700, Charles Smith wrote:
> Hi.
>
> How can I say, from the cmd line, that python should take my CWD as my
> CWD, and not the directory where the script actually is?
*scratches head*
Python does use your current working directory as your current working
directory. I think you are misdiagnosing the problem.
Here's a demonstration:
steve@runes:~$ cat test.py
import os
print os.getcwd()
steve@runes:~$ ln -s ~/test.py /tmp/test
steve@runes:~$ ls -l /tmp/test
lrwxrwxrwx 1 steve steve 19 May 16 18:58 /tmp/test -> /home/steve/test.py
steve@runes:~$ cd /etc/
steve@runes:/etc$ python /tmp/test
/etc
> I have a python script that works fine when it sits in directory WC, but
> if I move it out of WC to H and put a symlink from H/script to WC, it
> doesn't find the packages that are in WC. Also, if I use the absolute
> path to H, it won't find them, but I guess I can understand that.
The obvious solution is to make sure that WC is in the Python path. You
can do that by adding it the environment variable PYTHONPATH, or by
adding it to sys.path at the start of your script. I think you can also
use a .pth file as well.
Another solution may be to add this to the beginning of your script:
os.setcwd('path/to/WC')
but that's a crappy solution, you generally don't want to be changing the
working directory from inside scripts if you can avoid it.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Charles Smith <cts.private.yahoo@gmail.com> |
|---|---|
| Date | 2013-05-16 02:23 -0700 |
| Message-ID | <216f1770-b7f0-44bb-9953-7a6d573663d5@dl10g2000vbb.googlegroups.com> |
| In reply to | #45416 |
On 16 Mai, 11:04, Steven D'Aprano <steve +comp.lang.pyt...@pearwood.info> wrote: > Python does use your current working directory as your current working > directory. I think you are misdiagnosing the problem. That's usually how it ends up ... > > Here's a demonstration: > > steve@runes:~$ cat test.py > import os > print os.getcwd() > > steve@runes:~$ ln -s ~/test.py /tmp/test > steve@runes:~$ ls -l /tmp/test > lrwxrwxrwx 1 steve steve 19 May 16 18:58 /tmp/test -> /home/steve/test.py > steve@runes:~$ cd /etc/ > steve@runes:/etc$ python /tmp/test > /etc You're right. I can believe that python doesn't really change it's notion of the CWD. It seems like when I'm in WC and invoke ./script (which is symlinked to H/script), python says, "I have a specification somewhere that says I can look for modules where my script is, and since I'm really smart, I happen to know that I'm in WC but the script is not, but is really in H, where the stupid operator better have his packages set up" > > > I have a python script that works fine when it sits in directory WC, but > > if I move it out of WC to H and put a symlink from H/script to WC, it > > doesn't find the packages that are in WC. Also, if I use the absolute > > path to H, it won't find them, but I guess I can understand that. > > The obvious solution is to make sure that WC is in the Python path. ... > but that's a crappy solution, Right. Other tools don't try to figure out what I really want to do with a symlink, but just accept that it's a file. Python should do the same ... But, as you say, I'm surely misdiagnosing the problem.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web