user3870787
user3870787

Reputation: 1

Can Not Run Python Script Unless I Am In The Directory Where The Python Script Resides

I am using Python 3.3.1 on Windows 7.

I can execute my python script with this command from the command line

C:\Users\gyorulmaz\workspace\Test_Automation\Source>python TestAutomation.py

however if I try launching python from a different location

C:\python C:\Users\gyorulmaz\workspace\Test_Automation\Source\TestAutomation.py

I get an error message

File "C:\Python33\lib\configparser.py", line 1136, in _unify_values
raise NoSectionError(section) configparser.NoSectionError: No section: 'automation'

I am guessing I need to configure something with my python environment.

Help on this would be much appreciated.

Upvotes: 0

Views: 1469

Answers (2)

abarnert
abarnert

Reputation: 365627

Although it's impossible to be sure when you don't show us any code, most likely the problem is that the script is trying to open a file in the same directory as the script (or in a subdirectory or other relative path).

For example:

f = open('spam.txt')

That's not a file in the same directory as the script, that's a file in the current working directory. So, unless the current working directory happens to be the directory the script is in, you will get a FileNotFoundError.

In your case, you're using configparser, which will treat a FileNotFoundError the same as an empty file, so it will successfully import an empty config—but then as soon as you ask for the 'automation' section, there will obviously be no such section, so you'll get a NoSectionError instead.


Normally, you're going to want to install a script somewhere, and install whatever data files it needs somewhere as well, rather than running them out of the development directory. Python has all kinds of ways to do that, but you're going to have to read a tutorial about packaging things up in Python (I think Hitchhiker's Guide to Packaging is still the recommended starting point, but that may be out of date, so don't quote me on it…) before I could even begin to explain how to package up data files.

But here, we're talking about config files. Normally, these just go to some fixed path relative to the user's home directory. So, for a quick&dirty solution, you could just pick some such home-relative path and hardcode it into your app. Or, to get even quicker and dirtier, if this is just a hacky script for your own use, just hardcode the absolute pathname.


If you really, really want to use a script-relative path, and don't want to hardcode it:

scriptdir = os.path.dirname(sys.argv[0])
configpath = os.path.join(scriptdir, 'my.cfg')

Unfortunately, sys.argv[0] is not guaranteed to be an absolute pathname, or even a pathname at all; it's allowed to be just the filename. In Python 3.4, you can solve this by using __file__ instead, but you're using 3.3, and that has other problems. (See this question for details.) And what if someone puts your script inside a zip archive and runs it out of the archive (which Python can do for you)? There is no perfect answer here, but the code above is probably what you want.

Upvotes: 3

AKS
AKS

Reputation: 17316

Are you running C:\python executable --OR running python at c:\ location?

Seems like you are not. You're running a file c:\python (which doesn't exist). If you'll run it from C:\, then you would have pasted it like C:\>python .... or never asked this ?.

Do this:
1. cd to C:\
2. Now you'll see C:> as prompt in DOS/Windows
3. Run the following and it should work.
python C:\Users\gyorulmaz\workspace\Test_Automation\Source\TestAutomation.py

Your screen will show it like:
C:\>python C:\Users\gyorulmaz\workspace\Test_Automation\Source\TestAutomation.py

OR if you did a typo, then you can seek help from Python - Find Path to File Being Run

Upvotes: 0

Related Questions