Forums

Can use installed packing in Bash but not in Web App

I am trying to import an installed module called "ftransc".

I installed the module using pip with my virtualenv activated.

As shown, it is accessible from Bash: https://photos.app.goo.gl/4BsaP8t6Gk1eamZa6

However, when I import it into my Flask App, I get a Module Not Found error: https://photos.app.goo.gl/gHv97YfSgk5wEPpu5

I specifically included numpy because there is no error when importing that one. What could I be doing wrong? Thank you for any help!

It looks like you've installed it into a virtualenv, and while you've correctly specified the virtualenv on the "Web" page, the editor isn't smart enough to join the dots and run it using that env when you click the run button.

There are two ways to address that:

  • Specify the path to the virtualenv in a "hashbang" at the start of the file -- see this help page; the bit at the bottom shows how to specify a virtualenv.
  • Close the console in your editor, then refresh the page, and then instead of using the normal "Run" button, click the "Bash console here" button that will appear where the console was. That will (of course :) start a bash console in the directory containing your file, so you can workon your virtualenv and then run the file with python hideyourlove.py.

Hi Giles, Thank you for the super quick response!

The second solution appears to work within the context of the Bash console (thanks!) but how can I make the virtualenv the default?

When I visit the site "in the wild", I still get the messages in the Server Logs: 2019-08-19 13:48:27 /bin/sh: 1: 2019-08-19 13:48:27 ftransc: not found

I assume the answer to this is the hashbang, though it doesn't seem to work. I used: #!/home/ryanlague/.virtualenvs/venv/bin/python3.7

You can activate a virtualenv in a Bash console with:

workon <virtualnenvname>

That error looks like you're trying the run ftransc as a program from within your web app. If you're doing that, you need to specify the full path to the file that you're trying to run.

Hi Glenn,

I'm very new to "web stuff" and only barely beyond "a n00b" as a coder so maybe I'm not totally understanding.

When I "workon venv", I am able to run the code properly from within that bash instance, and that's great. What I really want though is for the code to work when visitors visit the site! Though I might be missing something, it seems to me that when a visitor is on the site, the code doesn't work.

To give more context, "ftransc" is a CLI which converts Wav files to Mp3. I am trying to run it using

subprocess.run()

Thank you guys very much for all your help. You seem to have an excellent support system and it is very appreciated!

Ah, I see -- that's a more complicated question :-)

When you specify a virtualenv on the "Web" page, it determines which version of Python is used to run your website's code specifically. But if you use subprocess to run something, it will be back in the global system environment. So you need to tell it to use the virtualenv too. Could you paste the exact code you're running with subprocess? Then perhaps we can suggest the specific changes you'd need to make to make it use the virtualenv.

Oh! I didn't realize that it would be back in the global environment!

The code is very simple. The following function converts a wav file to an mp3 file and saves it with the same path as the original wav (but with a .mp3 extension):

def waveToMp3(wav):
    cmd = f"ftransc -f mp3 {wav} -w -q low"
    subprocess.call(cmd, shell=True)

None of the arguments really matter. Here is the simplest possible version:

def useModule():
    subprocess.call("ftransc --help", shell=True)

Use which ftransc in a Bash console with the virtualenv activated to find the full path to ftransc. Then, to test it, try running that full path in a Bash console without the virtualenv activated. If that works, then use that full path in your subprocess call.

Worked like a charm. Thanks again!

Glad we could help :-)