Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 29 characters in body
Source Link
Column01
  • 151
  • 1
  • 13

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If you run that function from the script testtest1.py directly, it will output the text file to /tests/test1.txt. If you were to then import test1.py into a script in the root directory and call the function, it will output the file to the root directory as test1.txtinstead.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If run from the script test.py, it will output the text file to /tests/test1.txt. If you then import test1.py into a script in the root directory and call the function, it will output the file to the root directory as test1.txt.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If you run that function from the script test1.py directly, it will output the text file to /tests/test1.txt. If you were to then import test1.py into a script in the root directory and call the function, it will output the file to the root directory instead.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

deleted 14 characters in body
Source Link
Column01
  • 151
  • 1
  • 13

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If run from the script test.py, it will output the text file to /tests/test1.txt. If you then import test1.py into a script in the root directory and call the function, it will output the file to the root directory as test1.txt.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If run from the script test.py, it will output the text file to /tests/test1.txt. If you then import test1.py into a script in the root directory and call the function, it will output the file to the root directory as test1.txt.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If run from the script test.py, it will output the text file to /tests/test1.txt. If you then import test1.py into a script in the root directory and call the function, it will output the file to the root directory as test1.txt.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

Source Link
Column01
  • 151
  • 1
  • 13

The easiest solution here is to not shadow built in modules with custom ones, and use absolute paths but regardless this might help you out with some more information:

Python will execute an imported module's function from the directory it is called from.

For example, a function called output_file is in the script test1.py in the directory tests/test1.py and will output a text file called test1.txt when run. If run from the script test.py, it will output the text file to /tests/test1.txt. If you then import test1.py into a script in the root directory and call the function, it will output the file to the root directory as test1.txt.

**Directory path for visual**
├── main.py
├── (if imported and run from main, "test1.txt" is here)
├── tests
│ ├── test1.py
│ ├── (if run from test1 directly, "test1.txt" is here)

All the import statements are executed like you would be importing from the main script (meaning for attempt 3 above, it will import the builtin math.py cause site.py gets run as if it was from the root directory, and doesn't have the absolute path to include/math.py.)

lang-py

AltStyle によって変換されたページ (->オリジナル) /