The following doesn't solve the OP's problem, but the title and error is exactly what I faced.
If your project has a setup.py
script in it, you can install that package you are in, with python3 -m pip install -e .
or python3 setup.py install
or python3 setup.py develop
, and this package will be installed, but still editable (so changes to the code will be seen when importing the package). If it doesn't have a setup.py
, make sense of it.
Anyway, the problem OP faces seems to not exist anymore?
file one.py
:
def function():
print("output")
file two.py
:
import one
one.function()
chmod +x two.py
./two.py
Command line output: output
Other solutions seem 'dirty'
In the case of OP with 2 test files, modifying them to work is probably fine. However, in other real scenarios, the methods listed in the other answers is probably not recommended. They require you to modify the python code or restrict your flexibility (running the python file from a specific directory) and generally introduce annoyances. What if you've just cloned a project, and this happens? It probably already works for other people, and making code changes is unnecessary. The chosen answer also wants people to run a script from a specific folder to make it work. This can be a source of long term annoyance, which is never good. It also suggests adding your specific python folder to PATH (can be done through python or command line). Again, what happens if you rename or move the folder in a few months? You have to hunt down this page again, and eventually discover you need to set the path (and that you did exactly this a few months ago), and that you simply need to update a path (sure you could use sys.path and programmatically set it, but this can be flaky still). Many sources of great annoyance.
import sys; sys.path