1

I am trying to set up a very simply python 2 build system in sublime text 3.

The default build environment for python works, but I want to be able to import top level modules of the project by adding the project folder to PYTHONPATH.

Here is my attempt at a build system:

{
  "cmd": ["python", "-u", "$file"],
  "env": 
  {
      "PYTHONPATH": "$project_path"
  },
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

The terminal pops up with:

Traceback (most recent call last):
  File "C:\project", line 4, in <module>
    from lib.util.utilities import pairwise
ImportError: No module named lib.util.utilities
[Finished in 0.3s]

It's not seeing the $project_path folder, otherwise it would be able to import utilities, as lib/ is a folder in the top level project folder. How can I fix this?

I am on x64 Windows 8.1

4
  • Is lib a module? I.e. does it contain a __init__.py file? Commented Jul 2, 2015 at 17:34
  • @ThinkChaos Yes, lib contains a __init__.py. Commented Jul 2, 2015 at 17:36
  • Are you sure your build system is the one being used? Commented Jul 2, 2015 at 17:39
  • Yes, it is the one that is selected under Tools->Build System->my_python2_build. Commented Jul 2, 2015 at 17:40

1 Answer 1

1

According to my tests, and to a post on Sublime Text's forums, variables are not expanded in env.

I found this by running the following Python code with your build system:

import os

print(os.environ['PYTHONPATH'])

Which outputs: $project_path.

Using the following build system will work, though you have the overhead of spawning a shell whenever you run your code:

{
    "shell_cmd": "export PYTHONPATH=\"$project_path\"; python -u \"$file\"",
    "windows":
    {
        "shell_cmd": "set \"PYTHONPATH=$project_path\" & python -u \"$file\"",
    },
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

This works for me on both OS X and Windows with the following setup:

te st
├── lib
│   ├── __init__.py
│   └── util.py
├── src
│   └── te st.py
└── test.sublime-project
Sign up to request clarification or add additional context in comments.

4 Comments

I get that 'export' is not recognized as an internal or external command, operable program or batch file. [Finished in 0.0s with exit code 1] is this a default windows shell command?
Right, Windows forgot about that; sorry!
@user27886 I fixed it to work with spaces on Windows too.
That's what happens with set "PYTHONPATH=$project_path". I added a working project structure for you to compare with yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.