How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

Posted on

Solving problem is about exposing yourself to as many situations as possible like How to add to the PYTHONPATH in Windows, so it finds my modules/packages? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about How to add to the PYTHONPATH in Windows, so it finds my modules/packages?, which can be followed any time. Take easy to follow this discuss.

How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

I have a directory which hosts all of my Django apps (C:My_Projects). I want to add this directory to my PYTHONPATH so I can call the apps directly.

I tried adding C:My_Projects; to my Windows Path variable from the Windows GUI (My Computer > Properties > Advanced System Settings > Environment Variables). But it still doesn’t read the coltrane module and generates this error:

Error: No module named coltrane

Asked By: darren

||

Answer #1:

You know what has worked for me really well on windows.

My Computer > Properties > Advanced System Settings > Environment Variables >

Just add the path as C:Python27 (or wherever you installed python)

OR

Then under system variables I create a new Variable called PythonPath. In this variable I have C:Python27Lib;C:Python27DLLs;C:Python27Liblib-tk;C:other-folders-on-the-path

enter image description here

This is the best way that has worked for me which I hadn’t found in any of the docs offered.

EDIT: For those who are not able to get it,
Please add

C:Python27;

along with it. Else it will never work.

Answered By: darren

Answer #2:

Windows 7 Professional
I Modified @mongoose_za’s answer to make it easier to change the python version:

  1. [Right Click]Computer > Properties >Advanced System Settings > Environment Variables
  2. Click [New] under “System Variable”
  3. Variable Name: PY_HOME, Variable Value:C:pathtopythonversion
    enter image description here
  4. Click [OK]
  5. Locate the “Path” System variable and click [Edit]
  6. Add the following to the existing variable:

    %PY_HOME%;%PY_HOME%Lib;%PY_HOME%DLLs;%PY_HOME%Liblib-tk;
    enter image description here

  7. Click [OK] to close all of the windows.

As a final sanity check open a command prompt and enter python. You should see

>python [whatever version you are using]

If you need to switch between versions, you only need to modify the PY_HOME variable to point to the proper directory. This is bit easier to manage if you need multiple python versions installed.

Answered By: Delicia Brummitt

Answer #3:

From Windows command line:

set PYTHONPATH=%PYTHONPATH%;C:My_python_lib

To set the PYTHONPATH permanently, add the line to your autoexec.bat. Alternatively, if you edit the system variable through the System Properties, it will also be changed permanently.

Answered By: Yuval Adam

Answer #4:

Just append your installation path (ex. C:Python27) to the PATH variable in System variables. Then close and open your command line and type ‘python’.

Answered By: Paulino III

Answer #5:

These solutions work, but they work for your code ONLY on your machine. I would add a couple of lines to your code that look like this:

import sys
if "C:\My_Python_Lib" not in sys.path:
    sys.path.append("C:\My_Python_Lib")

That should take care of your problems

Answered By: inspectorG4dget

Answer #6:

Adding Python and PythonPath to the Windows environment:

  1. Open Explorer.
  2. Right-click ‘Computer’ in the Navigation Tree Panel on the left.
  3. Select ‘Properties’ at the bottom of the Context Menu.
  4. Select ‘Advanced system settings’
  5. Click ‘Environment Variables…’ in the Advanced Tab
  6. Under ‘System Variables’:

    1. Add

      • PY_HOME

        C:Python27
        
      • PYTHONPATH

        %PY_HOME%Lib;%PY_HOME%DLLs;%PY_HOME%Liblib-tk;C:another-library
        
    2. Append

      • path

        %PY_HOME%;%PY_HOME%Scripts
        
Answered By: Mr. Polywhirl

Answer #7:

The easier way to set the path in python is :
click start> My Computer >Properties > Advanced System Settings > Environment Variables >
second windows >

enter image description here

select Path > Edit > and then add “;C:Python27;C:Python27Scripts”

Leave a Reply

Your email address will not be published.