top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

executing python scripts that are symlinked

+1 vote

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.

posted May 16, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
Best answer

Python does use your current working directory as your current working directory. I think you are misdiagnosing the problem.

Here's a demonstration:

$ cat
import os
print os.getcwd()

$ ln -s ~/ /tmp/test
$ ls -l /tmp/test 
lrwxrwxrwx 1 ...    ... 19 May 16 18:58 /tmp/test -> /home/steve/
$ cd /etc/
$ python /tmp/test 

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:


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.

answer May 16, 2013 by anonymous
0 votes

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.

answer May 16, 2013 by anonymous
Similar Questions
0 votes

How can write a python script which will execute some perl scripts in a gui and shouldn't be post processed....?

+2 votes

Basically, is it possible to compile multiple unrelated python scripts into a single exe file, so when execute it several python programs are run at once.

In order to use this on another machine without python installed and only by using one single file.

+2 votes

Python scripts can run without a main(). What is the advantage to using a main()? Is it necessary to use a main() when the script uses command line arguments? (See script below)


import sys

def main():
 # print command line arguments
 for arg in sys.argv[1:]:
 print arg

if __name__ == "__main__":
0 votes

This is my first time using pyinstaller. My goal is to build an .app in Mac. The app is basically a GUI written in PySide, and I have about 7 different Python scripts + 1 .png file. The main file calls 4 of the files, and the 4 files will call the rest of the 2 files repeatedly. The .png file is nothing but the window logo. Could someone help me with some diagnosis? I do not know what went wrong. I searched a lot of documentations online, i.e., change spec, add import, ... etc. but my app still doesn't run.FYI, Pyinstaller could generate an app for me, but there are two issues:
1. Icon is not changed for the app.
2. App crashes when opened.

My Python version is 2.7.5 and I am using PyInstaller-2.0. Here is my code for packaging:

python --onefile --windowed --name=MyApplication -i ~/Documents/AASource/icon.ico ~/Documents/AASource/Scripts/main_file.pyHere is the spec file:

$ -*- mode: python -*- a = Analysis(['/Users/boxuancui/Documents/AASource/Scripts/'], pathex=['/Users/boxuancui/Documents/pyinstaller-2.0'], hiddenimports=[], hookspath=None) pyz = PYZ(a.pure) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name=os.path.join('dist', 'MyApplication'), debug=False, strip=None, upx=True, console=False , icon='/Users/boxuancui/Documents/AASource/icon.ico') app = BUNDLE(exe, name=os.path.join('dist', ''))Here is part of the crash message:

Crashed Thread: 0 Dispatch queue: Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0x00000000000054d8

Thanks in advance! Any help will be appreciated!

0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..