0

TL;DR

What are the options on Windows 10 to run from command line a python script specified by a relative path? Can environment variables be changed to do so? How can this be done?

LONG

I am using Windows 10. When I follow this installation guide (written for Linux), everything is fine until I need to perform the following on Windows

# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

On Windows, I added the two paths (the entire, absolute, path) to PYTHONPATH. When I execute the following; I see the extra two paths added.

C:\User\me>    python -c "import sys; print(sys.path)"

However, if I create a simple test.py script

print("Relative path works")

and run it from the command line:

C:\User\me> python test.py

it works if the test.py file is saved in C:\User\me\, but NOT when it is saved in the folders of PYTHONPATH.

This SO Q&A says that

PYTHONPATH is used by the python interpreter. It is not the same as Windows' PATH environment variable. You can't use it as a search path for passing files to the interpreter on the command line.

But I could not find how I can achieve what I need, using relative path of script to run it from command line. Please note that using the absolute path is not an option, because the instalation guide requires 2 folders are added to the PYTHONPATH, in order for the task to work.

It should be possible but I did not find any answer - Python 3 documentation describes Command line option for running script with relative path:

Execute the Python code contained in script, which must be a filesystem path (absolute or relative) referring to either a Python file...

DatamineR
  • 245
  • 5
  • 12
  • `PYTHONPATH` has nothing to do with finding a regular script run from the command line. It adds directories to `sys.path` for use with `import` statements. It's only relevant to command-line usage with the `-m` option that runs an imported module as a script or `-c` option that runs a command-line script. – Eryk Sun May 13 '18 at 00:24

2 Answers2

0

These lines from the install guide are intended to be run after a cd .../tensorflow/models/research/ and thus are absolute paths:

# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

You will likely need to do something similar on Windows. The equivalent would likely be to add the absolute paths to the libraries to PYTHONPATH environment variable.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • I should have been clearer - I added absolute paths to the PYTHONPATH environment variable. But as described above, it still not work. – DatamineR May 12 '18 at 22:37
0

I haven't tested this but as mentioned in here, this >>> export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim should be used as

set PYTHONPATH=%PYTHONPATH%;%cd%;%cd%\slim

Zo0418
  • 1
  • 2
  • That adapts the linked tensorflow setup for Windows. However, the OP's question implies a deep misunderstanding of what `PYTHONPATH` is for. The given "test.py" script is just a `print` call that has nothing to do with tensorflow, and the OP seems to mistakenly think that Python will search `sys.path` for this script. – Eryk Sun May 13 '18 at 00:31
  • correct, the test.py has nothing to do with tensor flow. It is only to test if I can run a simple script with relative path. Is there a way to do it? Can some setting of environment variables achieve that? – DatamineR May 13 '18 at 09:26
  • there are two options: 1) install the directory in a [development mode] (https://packaging.python.org/tutorials/distributing-packages/#working-in-development-mode). 2) when executing >>> python scriptName.py... if you supply directory instead of a file, the directory will be added to sys.path and --main--.py (- symbol used instead of _) file is executed as __main__>>> python dirName ...should do the job if you have a script named --main-- .py (- symbol used instead of _) in your directory. Refer [here] (https://docs.python.org/3/using/cmdline.html) – Zo0418 May 13 '18 at 18:32