CiaranWelsh
CiaranWelsh

Reputation: 7691

Why won't my system acknowledge a new path environment variable?

I am using the GUI method (rather than setx) to set a directory in the %PATH% environment variable on windows 7. My aim is to enable all my python scripts, modules and classes that I place in my 'D:\MPhil\Python\My_Python_Modules' directory to be available to use in the command line or another python script, but from any directory (much like the native python modules can be called from anywhere).

I have been through the process of: 'Edit the system environment variables->Environment variables->add my directory to the start of the %PATH% variable'

such that it now looks like this

'D:\MPhil\Python\My_Python_Modules\;C:\Program Files (x86)\ActiveState Komodo IDE 8\;...'

However, the 'Identify_Gene.py' script contained within the directory is still not callable (using the command 'python Identify_Gene.py', from a random directory.

Does anybody know what I am doing wrong?

Upvotes: 0

Views: 417

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149155

You are making a confusion between the PATH environment variable, which is where the shell (cmd.exe) searches for commands and the PYTHONPATH environment variable which initializes the sys.path variable that Python uses to find modules.

You can try 2 ways of doing (more or less) what you want :

  • Windows normaly knows that .py and .pyw files must be executed through the Python interpreter. You can prepend the directory where you put your script to the PATH (like you did) and call your script directly (and not python Identify_Gene.py)
    Identify_Gene.py
  • put only the directory containing python in the PATH and put the directory containing your scripts into PYTHONPATH (you can create new environment variables under Windows if it does not exists). You can the call your scripts as modules :
    python -m Identify_Gene

Upvotes: 1

rook
rook

Reputation: 6270

You can do the following:

  1. Add to Windows environment varibale PATHEXT python script extension:.PY

    set PATHEXT=%PATHEXT%;.PY

    (use Env. Settings GUI to make it permament)

  2. In command line: register that python extension:

    assoc .py=Python.File

  3. In command line: add application invoker to the extension:

    ftype Python.File=<path to python exe>\pythonw.exe "%%1" %%*

Next time you could simply run your script by name in the command line and without .py extension (if you set your PATH variable).

Another option: you should add path of your scripts folder to the PYTHONPATH variable and then you could invoke your scripts in the form:

python -m <script name>

Upvotes: 1

Related Questions