Deploying a python application with shared package

I'm thinking how to arrange a deployed python application which will have a

  1. Executable script located in /usr/bin/ which will provide a CLI to functionality implemented in
  2. A library installed to wherever the current site-packages directory is.

Now, currently, I have the following directory structure in my sources:

foo.py
foo/
  __init__.py
  ...

which I guess is not the best way to do things. During development, everything works as expected, however when deployed, the "from foo import FooObject" code in foo.py seemingly attempts to import foo.py itself, which is not the behaviour I'm looking for.

So the question is what is the standard practice of orchestrating situations like this? One of the things I could think of is, when installing, rename foo.py to just foo, which stops it from importing itself, but that seems rather awkward...

Another part of the problem, I suppose, is that it's a naming challenge. Perhaps call the executable script foo-bin.py?


Asked by: Kelsey262 | Posted: 01-10-2021






Answer 1

This article is pretty good, and shows you a good way to do it. The second item from the Do list answers your question.

shameless copy paste:

Filesystem structure of a Python project

by Jp Calderone

Do:

  • name the directory something related to your project. For example, if your project is named "Twisted", name the top-level directory for its source files Twisted. When you do releases, you should include a version number suffix: Twisted-2.5.
  • create a directory Twisted/bin and put your executables there, if you have any. Don't give them a .py extension, even if they are Python source files. Don't put any code in them except an import of and call to a main function defined somewhere else in your projects.
  • If your project is expressable as a single Python source file, then put it into the directory and name it something related to your project. For example, Twisted/twisted.py. If you need multiple source files, create a package instead (Twisted/twisted/, with an empty Twisted/twisted/__init__.py) and place your source files in it. For example, Twisted/twisted/internet.py.
  • put your unit tests in a sub-package of your package (note - this means that the single Python source file option above was a trick - you always need at least one other file for your unit tests). For example, Twisted/twisted/test/. Of course, make it a package with Twisted/twisted/test/__init__.py. Place tests in files like Twisted/twisted/test/test_internet.py.
  • add Twisted/README and Twisted/setup.py to explain and install your software, respectively, if you're feeling nice.

Don't:

  • put your source in a directory called src or lib. This makes it hard to run without installing.
  • put your tests outside of your Python package. This makes it hard to run the tests against an installed version.
  • create a package that only has a __init__.py and then put all your code into __init__.py. Just make a module instead of a package, it's simpler.
  • try to come up with magical hacks to make Python able to import your module or package without having the user add the directory containing it to their import path (either via PYTHONPATH or some other mechanism). You will not correctly handle all cases and users will get angry at you when your software doesn't work in their environment.

Answered by: Blake359 | Posted: 02-11-2021



Answer 2

Distutils supports installing modules, packages, and scripts. If you create a distutils setup.py which refers to foo as a package and foo.py as a script, then foo.py should get installed to /usr/local/bin or whatever the appropriate script install path is on the target OS, and the foo package should get installed to the site_packages directory.

Answered by: Arthur637 | Posted: 02-11-2021



Answer 3

You should call the executable just foo, not foo.py, then attempts to import foo will not use it.

As for naming it properly: this is difficult to answer in the abstract; we would need to know what specifically it does. For example, if it configures and controls, calling it -config or ctl might be appropriate. If it is a shell API for the library, it should have the same name as the library.

Answered by: Darcy201 | Posted: 02-11-2021



Answer 4

Your CLI module is one thing, the package that supports it is another thing. Don't confuse the names withe module foo (in a file foo.py) and the package foo (in a directory foo with a file __init__.py).

You have two things named foo: a module and a package. What else do you want to name foo? A class? A function? A variable?

Pick a distinctive name for the foo module or the foo package. foolib, for example, is a popular package name.

Answered by: Maddie860 | Posted: 02-11-2021



Similar questions

c++ - Deploying application with Python or another embedded scripting language

I'm thinking about using Python as an embedded scripting language in a hobby project written in C++. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this. Is it feasible to deploy a Python interpreter + standard library with my application? Would some othe...


python - Deploying a Web.py application with WSGI, several servers

I've created a web.py application, and now that it is ready to be deployed, I want to run in not on web.py's built-in webserver. I want to be able to run it on different webservers, Apache or IIS, without having to change my application code. This is where WSGI is supposed to come in, if I understand it correctly. However, I don't understand what exacly I have to do to make my application deployable on a WSGI server? M...


python - What is the best practice in deploying application on Windows?

I have an application that consists of several .dlls, .libs, .pyd (python library), .exe, .class-es. What is the best practice in the deployment process? I plan to put .dlls - managed into GAC and unmanaged into WinSxS folder. What should I do with .libs, .exe, .class and .pyd? Is it ok to put it to /ProgramFiles/ApplicationName/bin /ProgramFiles/ApplicationName/lib /Program...


hosting - Creating and deploying a python chat application using Twisted

I have created a chat server application using the Twisted framework. I am running it on my local machine and now I want to go global. The application is similar to omegle.com. How can I develop on a third party commercial server so that it runs continuously? Do I need to get a dedicated server for it?


python - Deploying a PyQt application on Windows Vista x64

I'm working on an application for a client/friend using PyQt. I've been working on Linux and testing on Vista, but the target computer is Vista x64. Now, Python comes with compiled binaries of Python 2.6 for 64 bit Windows, but Riverbank don't provide 64 bit binaries for PyQt. I don't have much access to the target computer, so I can't really go through the


python - Problem with deploying django application on mod_wsgi

Closed. This question is off-topic. It is not curre...


Easy Deploying of Python and application in one Bundle , For Linux

I Develop Fairly large python application on server side , with all database connect , files extraction , parsing , command line calls. It becomes a nightmare for deploying as i used many third party modules outside of standard python lib. And i lost track of them . Especially Differnt Linux OS uses different version of them so it is no longer good to install them using OS's package manager. I want to depl...


python - Deploying a Flask application with CGI

This question already has answers here:


deploying a python web application


python - Deploying a Flask application using Paste

I'm building a web service using Flask and I'm trying to deploy a simple "Hello, World" app using Paster. I'm having trouble get everything configured to work together though. I've seen the Google hit about running Flask with paste using virtualenv and zcbuildout, but that seems like it's overkill for a pretty basic application. Right now, when I try to load a URL with my app, I get this error: Traceback (m...


python - Why won't Django 1.0 admin application work?

I've just started playing with Django and am loosely following the tutorial with my own set of basic requirements. The models I've sketched out so far are a lot more comprehensive than the tutorial, but they compile fine. Otherwise, everything should have been the same. My problem is with the admin application. I can log into it, and view the editable models, but when I click on a model or any of the change/add but...


python - What is the best way to serve static web pages from within a Django application?

I am building a relatively simple Django application and apart from the main page where most of the dynamic parts of the application are, there are a few pages that I will need that will not be dynamic at all (About, FAQ, etc.). What is the best way to integrate these into Django, idealing still using the Djang...


networking - Embedding a remote Python shell in an application

You can embed the IPython shell inside of your application so that it launches the shell in the foreground. Is there a way to embed a telnet server in a python app so that you can telnet to a certain port and launch a remote IPython shell? Any tips for redirecting the input/output streams for IPython or how to hook it up to a telnet server library or recom...


python - Debug Pylons application through Eclipse

I have Eclipse setup with PyDev and love being able to debug my scripts/apps. I've just started playing around with Pylons and was wondering if there is a way to start up the paster server through Eclipse so I can debug my webapp?


python - How can I capture all exceptions from a wxPython application?

I'm writing a little debug app for a bit of kit we're developing and I'd like to roll it out to a few users to see if they can provoke any crashes. Does anyone know a way of effectively wrapping a wxPython app to catch any and all unhandled exceptions that would cause the app to crash? Ideally I'd want to capture all output (not just errors) and log it to a file. Any unhandled exceptions ought to log to the current...


deployment - How do I deploy a Python desktop application?

I have started on a personal python application that runs on the desktop. I am using wxPython as a GUI toolkit. Should there be a demand for this type of application, I would possibly like to commercialize it. I have no knowledge of deploying "real-life" Python applications, though I have used py2exe in the past with varied success. How would I obfu...


Starting a new database driven python web application would you use a javascript widget framework? If so which framework?

I am starting a new web application project. I want to use python as I am using it at my bread-and-butter-job. However I don't want to reinvent the wheel. Some things I have thought about: AJAX would be nice if it’s not too much of a hazzle. It is best if the licensing allows commercialization but is not crucial at this point. It could also be funny to try out the G...


console application - using curses with raw_input in python

In my python linux console application I use curses to handle displaying of data. At the same time I'd like to have an input line to enter commands, pretty much in good ol' irssi-style. With default curses getch() I'd have to do a lot of coding just to get the basic funcionality of raw_input function - arrow keys to move cursor / browse through the input history. Is there a simple way to get such behavior working w...


scripting - How can I launch an instance of an application using Python?

I am creating a Python script where it does a bunch of tasks and one of those tasks is to launch and open an instance of Excel. What is the ideal way of accomplishing that in my script?


Global hotkey for Python application in Gnome

I would like to assign a global hotkey to my Python application, running in Gnome. How do I do that? All I can find are two year old posts saying, well, pretty much nothing :-)






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top