2

Check project tree. Hello. I'm trying to run example.py , but have problems with importing src. It can't find module. I can solve this problem, writing:

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

now sys.path have path to folder of the project D:\...\cubic_reg-master, and may check all packages and modules in project. But I don't want to write it in every folder/every script. Trying to find how to config Build System or project.sublime-settings.

p.s. Have worked in Pycharm, and trying Sublime Text, as I understand Pycharm add path automatically.

2 Answers 2

1

You really should rarely ever have to change your PYTHONPATH. It's a bit hard to understand your question, but I am assuming you want to add sublime text to your regular PATH so you can call a project in sublime text from command line. To do that see this article...

For OSX: https://www.sublimetext.com/docs/3/osx_command_line.html

For Windows: Sublime Text from Command Line (Win7) see first answer

Sign up to request clarification or add additional context in comments.

2 Comments

doesn't answer OP's question
No, I'm trying to make comfortable IDE with sublime text. So, I created sublime project, made custom system build with anaconda 3. (sublimetext.info/docs/en/reference/build_systems.html). Now I want sublime text to add project directory to sys.path. Then package examples will be able to find package src. tried // "env": // { // "PYTHONPATH": "$project_path", // },
1

who seeks will always find

Decision is to run python with arg -c; make there settings and run file

Here is mine Build System file:

{
   "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
   "selector": "source.python",
   "shell": "true",
   "windows":
   {
           "cmd": ["D:\\Development\\Python\\Anaconda3\\python", "-c", "import sys; sys.path.insert(0, '$project_path'); exec(open('$file_name', 'r').read());"]
   }

}

p.s. probably it's dangerous solution, I will be glad to look at better ways

p.p.s. for plugin sublime Repl same settings work, but should use $folder and $file_basename:

BrowsePackages -> SublimeREPL\config\Python\Main.sublime-menu
{...
    "cmd": ["D:\\Development\\Python\\Anaconda3\\python", "-i", "-c", "import sys; sys.path.insert(0, '$folder'); exec(open('$file_basename', 'r').read());"],

... }

----------------------------------------------------------------------------------------------------------------------------------- six months later

-----------------------------------------------------------------------------------------------------------------------------------

Found better decisions:

1) build system have option 'env' first idea is to write "env": {"PYTHONPATH": "$project_path"}, but it don't know such variable $project_path for env opt, so this works

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

disadvantages: incorrect working_dir - if you create or read files - should consider it

2) better way is to use some commands in cmd:

{
   "windows":
   {
      "shell": "true",
      "cmd":"set PYTHONPATH=$project_path & D:\\Development\\Python\\Anaconda3\\python -u $file",
   },
   "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
   "selector": "source.python",
}

3) add PYTHONPATH to mine_proj.sublime-project with plugin "environment settings"

use there real path or %project_path% (.sublime project stores in project, to use %project_path% turn on "set_sublime_variables")

EnvironmentSettings - User

{
    "set_sublime_variables": true
}

mine_proj.sublime-project: add only "settings" part

{
    "build_systems":
    [
        {
            "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
            "name": "Anaconda Python Builder",
            "selector": "source.python",
            "shell_cmd": "\"python\" -u \"$file\""
        }
    ],
    "settings":
    {
        "env":
        {
          "Windows":
          {
                "PYTHONPATH": "%project_path%"
          },
        }
    },
    "folders":
    [
        {
            "path": "."
        }
    ]
}

Comments

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.