# Running python from the command tool. Behaves wierdly

**URL:** <https://discourse.mapeditor.org/t/running-python-from-the-command-tool-behaves-wierdly/2277>\
**Category:** Bugs\
**Created:** [April 10, 2017, 2:16pm UTC](https://discourse.mapeditor.org/t/running-python-from-the-command-tool-behaves-wierdly/2277 "2017-04-10T14:16:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rickard\_Andersson](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@Rickard\_Andersson](https://discourse.mapeditor.org/u/Rickard_Andersson)\
**Post date:** [April 10, 2017, 2:16pm UTC](https://discourse.mapeditor.org/t/running-python-from-the-command-tool-behaves-wierdly/2277/1 "2017-04-10T14:16:12Z")

</div>

Hello!  
Im running a small python script from the command tool and some modules does not seem to work: sys.path and logging.  
Im on Windows 10, Tiled 0.18.2 and Python 2.7.  
My script works when running it from cmd with the arguments that I’ve set up Tiled to run it with. It works when running from Tiled when commenting out the calls to any functionality in sys.path or logging (the modules can be imported without failing)  
Also I dont get any console wich is the reason i want to use logging to print error messages to a file.

Thanks for a really nice tool 🙂

---

<div class="post-metadata">

**Author:** ![Rickard\_Andersson](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@Rickard\_Andersson](https://discourse.mapeditor.org/u/Rickard_Andersson)\
**Post date:** [April 21, 2017, 2:50pm UTC](https://discourse.mapeditor.org/t/running-python-from-the-command-tool-behaves-wierdly/2277/2 "2017-04-21T14:50:41Z")

</div>

Im just gonna put the following here in case someone has the same problems:  
Its just a path issue. When running from the command tool the working directory is the installation directory of Tiled. Probably python does not have rights to create a file i that directory so logging would then fail.  
Easily fixed by changing the working dir to same as the script:  
`os.chdir(os.path.dirname(os.path.abspath(file)))`

Or you can just make a bat script and run that:  
`cd "C:\Users\me\path\to\my\game\scripts"
python “my_script.py” %1
`

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [April 21, 2017, 8:10pm UTC](https://discourse.mapeditor.org/t/running-python-from-the-command-tool-behaves-wierdly/2277/3 "2017-04-21T20:10:42Z")

</div>

Hey @Rickard_Andersson, thanks for letting us know what was causing your problem! Eventually I also hope to allow setting the working directory per command, which is what this issue is about:

> <https://github.com/mapeditor/tiled/issues/941>
>
> It would be nice if the working directory could be specified for the custom comm…ands.
> 
> Suggested magic variables:
> \- \`%executablePath\` - the path of the executable that's supposed to be run
> \- \`%mapPath\` - the path of the current map file
> 
> Originally requested at issue #940.

> [@Rickard\_Andersson](#):
>
> Also I dont get any console which is the reason i want to use logging to print error messages to a file.

On Linux there is a somewhat hidden “Run in terminal” action, but I guess in general it would be useful to print the output of the command into the Debug Console window. I’ve created the following issue for this:

> <https://github.com/mapeditor/tiled/issues/1552>
>
> Tiled has a Debug Console window, but it isn't really used. The only thing it is… used for at the moment is to allow writing some debug lines from Python plugins.
> 
> It would be useful to write the output from custom commands also to the Debug Console.
> 
> This would avoid the need for workarounds like \[described in this forum topic\](http://discourse.mapeditor.org/t/running-python-from-the-command-tool-behaves-wierdly/2277?u=bjorn).
