1

I'm currently launching my website and I want my Python script to be run. However, it does not work when I run it because it depends on 5 libraries. How do you link them to your code? For example, I use pymysql to write data to my host's database. However, it does not recognise pymysql. Is there a way to download all of the packages once on the server and then being able to access them? How to link to them in the code?

Many thanks !

sammtt
  • 401
  • 1
  • 6
  • 14

3 Answers3

1

There are many ways to do this, the easiest way is probably to use pip freeze > requirements.txt to get a list (requirements.txt) of the dependencies that you have installed for your project (which, if you're running under a virtualenv, is only those installed for your project).

If you have installed multiple libraries for your interpreter that you don't need, you can remove them from the list, or create the list manually.

You can make pip install all the libraries again on your host by doing pip -r requirements.txt.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • Thank you for your answer. Would you have the steps to access your server and to "pip install" my packages there? – sammtt Sep 10 '17 at 22:26
  • Usually that would be by logging in to the server through SSH, but there's probably admin panels that lets you invoke commands directly on the server as well. Depends on how your host has these things set up. – MatsLindh Sep 11 '17 at 06:56
  • I'm facing an issue in the http.client.py which is "Traceback (most recent call last): File "./data.py", line 9, in from urllib.request import urlopen, Request File "/home/www/urllib/request.py", line 88, in import http.client File "/home/www/http/client.py", line 1063 chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \ ^ SyntaxError: invalid syntax SyntaxError: invalid syntax1" – sammtt Sep 11 '17 at 21:40
  • I really can't understand what's wrong in this file. I've uploaded the latest version of http.client.py – sammtt Sep 11 '17 at 21:41
  • Please ask new questions as a new question and add your code properly formatted. – MatsLindh Sep 12 '17 at 07:02
  • How do you put all the libraries listed in requirements.txt into a bundle along with your Python script – sammtt Sep 12 '17 at 19:50
  • If you can't run pip on the host, you can create your own directory for libraries, bundle the libraries there and inject it into the import path when starting your script. – MatsLindh Sep 13 '17 at 08:14
1

The best way is to package your project and deploy it on your remote server.

The best practices nowadays is described in “Packaging and Distributing Projects” from the Python Packaging Authority

You can follow the Sample Python project available on GitHub.

Another way is to create a library or an application using a project template. One of the famous ones is cookiecutter-pypackage from Audrey Roy Greenfeld Cookiecutter.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
0

This is the info you're looking for:

https://www.namecheap.com/support/knowledgebase/article.aspx/9587/29/how-to-run-python-scripts

They give two solutions - the cPanel solution and the SSH solution.

hanumanDev
  • 6,592
  • 11
  • 82
  • 146