Skip to content
This repository was archived by the owner on Jan 27, 2023. It is now read-only.

Commit d3dfb28

Browse files
Make sure to fix import paths in every file that imports something local.
I'd be super happy if someone had shown me a cleaner way to do this
1 parent 830c657 commit d3dfb28

6 files changed

Lines changed: 72 additions & 0 deletions

File tree

search_in_project.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import inspect
77

8+
### Start of fixing import paths
89
# realpath() with make your script run, even if you symlink it :)
910
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]))
1011
if cmd_folder not in sys.path:
@@ -18,6 +19,7 @@
1819
# __file__ fails if script is called in different ways on Windows
1920
# __file__ fails if someone does os.chdir() before
2021
# sys.argv[0] also fails because it doesn't not always contains the path
22+
### End of fixing import paths
2123

2224
import searchengines
2325

searchengines/ack.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
### Start of fixing import paths
2+
import os, sys, inspect
3+
# realpath() with make your script run, even if you symlink it :)
4+
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
5+
if cmd_folder not in sys.path:
6+
sys.path.insert(0, cmd_folder)
7+
# use this if you want to include modules from a subforder
8+
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
9+
if cmd_subfolder not in sys.path:
10+
sys.path.insert(0, cmd_subfolder)
11+
# Info:
12+
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
13+
# __file__ fails if script is called in different ways on Windows
14+
# __file__ fails if someone does os.chdir() before
15+
# sys.argv[0] also fails because it doesn't not always contains the path
16+
### End of fixing import paths
17+
118
import base
219

320

searchengines/find_str.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
### Start of fixing import paths
2+
import os, sys, inspect
3+
# realpath() with make your script run, even if you symlink it :)
4+
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
5+
if cmd_folder not in sys.path:
6+
sys.path.insert(0, cmd_folder)
7+
# use this if you want to include modules from a subforder
8+
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
9+
if cmd_subfolder not in sys.path:
10+
sys.path.insert(0, cmd_subfolder)
11+
# Info:
12+
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
13+
# __file__ fails if script is called in different ways on Windows
14+
# __file__ fails if someone does os.chdir() before
15+
# sys.argv[0] also fails because it doesn't not always contains the path
16+
### End of fixing import paths
17+
118
import base
219

320

searchengines/git_grep.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
### Start of fixing import paths
2+
import os, sys, inspect
3+
# realpath() with make your script run, even if you symlink it :)
4+
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
5+
if cmd_folder not in sys.path:
6+
sys.path.insert(0, cmd_folder)
7+
# use this if you want to include modules from a subforder
8+
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
9+
if cmd_subfolder not in sys.path:
10+
sys.path.insert(0, cmd_subfolder)
11+
# Info:
12+
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
13+
# __file__ fails if script is called in different ways on Windows
14+
# __file__ fails if someone does os.chdir() before
15+
# sys.argv[0] also fails because it doesn't not always contains the path
16+
### End of fixing import paths
17+
118
import base
219

320

searchengines/grep.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
### Start of fixing import paths
2+
import os, sys, inspect
3+
# realpath() with make your script run, even if you symlink it :)
4+
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
5+
if cmd_folder not in sys.path:
6+
sys.path.insert(0, cmd_folder)
7+
# use this if you want to include modules from a subforder
8+
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))
9+
if cmd_subfolder not in sys.path:
10+
sys.path.insert(0, cmd_subfolder)
11+
# Info:
12+
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!
13+
# __file__ fails if script is called in different ways on Windows
14+
# __file__ fails if someone does os.chdir() before
15+
# sys.argv[0] also fails because it doesn't not always contains the path
16+
### End of fixing import paths
17+
118
import base
219

320

searchengines/the_silver_searcher.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
### Start of fixing import paths
12
import os, sys, inspect
23
# realpath() with make your script run, even if you symlink it :)
34
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
@@ -12,6 +13,7 @@
1213
# __file__ fails if script is called in different ways on Windows
1314
# __file__ fails if someone does os.chdir() before
1415
# sys.argv[0] also fails because it doesn't not always contains the path
16+
### End of fixing import paths
1517

1618
import base
1719

0 commit comments

Comments
 (0)