Skip to main content
added 973 characters in body; edited tags
Source Link
Felix
  • 377
  • 2
  • 16

I'd want to deploy my Python code and relevant static files such that only a copy of a folder is needed. That is, all the paths inside are relative. The release is to a web server, which calls scripts in a subprocess.

Naturally, everything works in my environment in PyCharm. Problems arise when I subprocess Python and execute scripts from an arbitrary working directory. This can be avoided a few ways:

  • Use absolute paths when referencing files
  • Use relative imports with submodules
  • Append paths to sys.path to use relative paths
  • Change the working directory of the host
  • Change project file structure

I have issues with these approaches.

  • Absolute paths won't work when the code is moved
  • Relative imports only work with submodules, not up the file tree
  • Appending to sys.path in the beginning of every script is ugly and cumbersome
  • Having all source files in the root of the project is messy

These might not be the best of reasons to complain, but in my experience, other people have more experience with almost all things, and might know a trick I've yet to encounter.

How would you approach this? (gasp, an opinion!) Many thanks! Any help is greatly appreciated.

Edit: Example file structures

Root\                       Root\                    Root\
    Data\                       Data\                    Data\
        Data.csv                    Data.csv                Data.csv
        Text.txt                    Text.txt                Text.txt
    Sources\                    Sources\
        Script.py                   Script.py            Script.py
        Module.py                   Module.py            Module.py
        Needs_data.py           Needs_data.py            Needs_data.py
        Data_and_module.py      Data_and_module.py       Data_and_module.py

These are some options I came up with to set up a file structure. Almost all scripts need to import a module and open files in Data. Ideally the structure would be the first kind, possibly with subfolders.

I'd want to deploy my Python code and relevant static files such that only a copy of a folder is needed. That is, all the paths inside are relative. The release is to a web server, which calls scripts in a subprocess.

Naturally, everything works in my environment in PyCharm. Problems arise when I subprocess Python and execute scripts from an arbitrary working directory. This can be avoided a few ways:

  • Use absolute paths when referencing files
  • Use relative imports with submodules
  • Append paths to sys.path to use relative paths
  • Change the working directory of the host

I have issues with these approaches.

  • Absolute paths won't work when the code is moved
  • Relative imports only work with submodules, not up the file tree
  • Appending to sys.path in the beginning of every script is ugly and cumbersome

These might not be the best of reasons to complain, but in my experience, other people have more experience with almost all things, and might know a trick I've yet to encounter.

How would you approach this? (gasp, an opinion!) Many thanks! Any help is greatly appreciated.

I'd want to deploy my Python code and relevant static files such that only a copy of a folder is needed. That is, all the paths inside are relative. The release is to a web server, which calls scripts in a subprocess.

Naturally, everything works in my environment in PyCharm. Problems arise when I subprocess Python and execute scripts from an arbitrary working directory. This can be avoided a few ways:

  • Use absolute paths when referencing files
  • Use relative imports with submodules
  • Append paths to sys.path to use relative paths
  • Change the working directory of the host
  • Change project file structure

I have issues with these approaches.

  • Absolute paths won't work when the code is moved
  • Relative imports only work with submodules, not up the file tree
  • Appending to sys.path in the beginning of every script is ugly and cumbersome
  • Having all source files in the root of the project is messy

These might not be the best of reasons to complain, but in my experience, other people have more experience with almost all things, and might know a trick I've yet to encounter.

How would you approach this? (gasp, an opinion!) Many thanks! Any help is greatly appreciated.

Edit: Example file structures

Root\                       Root\                    Root\
    Data\                       Data\                    Data\
        Data.csv                    Data.csv                Data.csv
        Text.txt                    Text.txt                Text.txt
    Sources\                    Sources\
        Script.py                   Script.py            Script.py
        Module.py                   Module.py            Module.py
        Needs_data.py           Needs_data.py            Needs_data.py
        Data_and_module.py      Data_and_module.py       Data_and_module.py

These are some options I came up with to set up a file structure. Almost all scripts need to import a module and open files in Data. Ideally the structure would be the first kind, possibly with subfolders.

Source Link
Felix
  • 377
  • 2
  • 16

Referencing custom Python modules and data files

I'd want to deploy my Python code and relevant static files such that only a copy of a folder is needed. That is, all the paths inside are relative. The release is to a web server, which calls scripts in a subprocess.

Naturally, everything works in my environment in PyCharm. Problems arise when I subprocess Python and execute scripts from an arbitrary working directory. This can be avoided a few ways:

  • Use absolute paths when referencing files
  • Use relative imports with submodules
  • Append paths to sys.path to use relative paths
  • Change the working directory of the host

I have issues with these approaches.

  • Absolute paths won't work when the code is moved
  • Relative imports only work with submodules, not up the file tree
  • Appending to sys.path in the beginning of every script is ugly and cumbersome

These might not be the best of reasons to complain, but in my experience, other people have more experience with almost all things, and might know a trick I've yet to encounter.

How would you approach this? (gasp, an opinion!) Many thanks! Any help is greatly appreciated.