Relative imports for the trilliоnth time

Script vs. Module Here’s an explanation. The short version is that there is a big difference between directly running a Python file, and importing that file from somewhere else. Just knowing what directory a file is in does not determine what package Python thinks it is in. That depends, additionally, on how you load the … Read more

How To Join Relative URLs in JavaScript

8 years later, many browsers (except for Internet Explorer) support the URL constructor (URL(url [, base])). > new URL(‘../address’, ‘http://www.adress.com/more/evenmore/’).href “http://www.adress.com/more/address” > new URL(‘../../address’, ‘http://www.adress.com/more/evenmore/’).href “http://www.adress.com/address”

In C#, how do I combine more than two parts of a file path at once?

Here’s a utility method you can use: public static string CombinePaths(string path1, params string[] paths) { if (path1 == null) { throw new ArgumentNullException(“path1”); } if (paths == null) { throw new ArgumentNullException(“paths”); } return paths.Aggregate(path1, (acc, p) => Path.Combine(acc, p)); } Alternate code-golf version (shorter, but not quite as clear, semantics are a bit … Read more

Struggling to append a relative path to my sys.path

The two below alternate possibilities apply to both Python versions 2 and 3. Choose the way you prefer. All use cases are covered. Example 1 main script: /some/path/foo/foo.py module to import: /some/path/foo/bar/sub/dir/mymodule.py Add in foo.py import sys, os sys.path.append(os.path.join(sys.path[0],’bar’,’sub’,’dir’)) from mymodule import MyModule Example 2 main script: /some/path/work/foo/foo.py module to import: /some/path/work/bar/mymodule.py Add in foo.py … Read more

VSCode Copy Relative Path with posix forward slashes

As of vscode 1.59 you can enable using the / separator even on Windows for the Copy Relative Path command. Explorer: Copy Relative Path Separator set to /. explorer.copyRelativePathSeparator auto (default): Uses operating system specific path separation character /: Use slash as path separation character \\: Use backslash as path separation character See https://github.com/microsoft/vscode/issues/56279 For … Read more

Rails redirect with https

The ActionController::Base#redirect_to method takes an options hash, one of the parameters of which is :protocol which allows you to call: redirect_to :protocol => ‘https://’, :controller => ‘some_controller’, :action => ‘index’ See the definition for #redirect_to and #url_for for more info on the options. Alternatively, and especially if SSL is to be used for all your … Read more

What does a dot mean in a URL path?

The path segment . is typically used at the begin of relative path references and are removed during the reference resolution, i.e. the process of resolving a relative URI reference to an absolute URI: The path segments “.” and “..“, also known as dot-segments, are defined for relative reference within the path name hierarchy. They … Read more

How do I use a relative path in a Python module when the CWD has changed?

Store the absolute path to the module directory at the very beginning of the module: package_directory = os.path.dirname(os.path.abspath(__file__)) Afterwards, load your resources based on this package_directory: font_file = os.path.join(package_directory, ‘fonts’, ‘myfont.ttf’) And after all, do not modify of process-wide resources like the current working directory. There is never a real need to change the working … Read more