How do I install and run a python script

kris

This is the ultimate noob question for python - but I'm asking it because I simply cannot fine the answer anywhere on the internet.

How do I install and run a python script?

For example, say, this repo: https://github.com/TineKolenik/hoffman_reproduction


I tried the following : pip3 install -r requirements.txt
But then I get a page of errors which begins with :

  Successfully uninstalled numpy-1.22.4
Running setup.py install for numpy ... error
ERROR: Command errored out with exit status 1:
 command: /Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 -u -c

'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/_n/dj0hlyp12xz_c2_9pjwgktt00000gn/T/pip-install-btfk24yw/numpy/setup.py'"'"'; file='"'"'/private/var/folders/_n/dj0hlyp12xz_c2_9pjwgktt00000gn/T/pip-install-btfk24yw/numpy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /private/var/folders/_n/dj0hlyp12xz_c2_9pjwgktt00000gn/T/pip-record-ffl_swjt/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /Users/kris/Library/Python/3.9/include/python3.9/numpy cwd: /private/var/folders/_n/dj0hlyp12xz_c2_9pjwgktt00000gn/T/pip-install-btfk24yw/numpy/ Complete output (5277 lines): Running from numpy source directory.

Note: if you need reliable uninstall behavior, then install
with pip instead of using `setup.py install`:

  - `pip install .`       (from a git repo or downloaded source
                           release)

pip3 install . returns:

Defaulting to user installation because normal site-packages is not writeable ERROR: Directory '.' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.

python3 main.py gives :

Traceback (most recent call last): File "/Users/kris/Projects/hoffman_reproduction/main.py", line 3, in import matplotlib.pyplot as plt ModuleNotFoundError: No module named 'matplotlib'


I've tried running python packages in the past and have never managed to get one to run.

Please help me out with the basic commands I need to install and run this repo - or pick another python repo, if there is something wrong with this one.

JarroVGIT

In Python, you don't have to install arbitrary scripts (such as your referenced git repo).

Couple of general pointers:

  • Python is an interpreter based language, installed on your machine. It's literally an executable. A specific piece of python code (e.g. a .py file) can be run by feeding it into your python installation. I am guessing you are using a Mac, so running "python3 file.py" in your command line would execute python, which would then execute file.py.
  • Python is very extensible. It consist out of many packages that provide functionality. This functionality can then be imported into your own code (with import package_name). Python comes with many in-built packages (for example, math, typing, re).
  • You can add packages to your Python installation by using pip. Pip is a command line tool to manage (e.g. find, install, uninstall, update) python packages. Well known packages that people tend to install are numpy, pandas, requests.

Knowing all that, using Python comes with a challenge. Executing python code is depending on the installation of Python itself of the machine it is running on. For example, on my Python installation, I have a package installed that is used in a python script. The script works perfectly well on my machine. However, if you were to run this script (and didn't have that specific package installed that the script is relying on), it would fail miserably with a ModuleNotFound error. So, we needed a way to communicate what dependencies a script has.

This is where requirements.txt came in. It describes what packages must be installed (and which version!) in your Python installation. You can then use pip to install said packages, after which you should be able to run the scripts.

Your situation

You found some python script that you want to run. Note that this is not a package that need to be installed, you just want to run main.py. However, this code is 4 years old (which is a long time, in terms of IT years). Specifically, from the top of my head, Python 3.5 was current back then (now we are at 3.10, you are running 3.9 which is completely fine by the way). That is important, as not everything is backward compatible.

The reason why I bring this is up is the following; you are trying (correctly!) to install requirements.txt, to ensure the right packages are installed in your Python installation that are used by this script. However, the versions of these packages are quite old and are not guaranteed to work on newer Python installations. And that is exactly the case here.

You already had the package installed in your Python installation (see the line Successfully uninstalled numpy-1.22.4), which pip uninstalled for you in order to try and install an older version (from requirements.txt: 1.14.3). That version is not compatible with your python installation.

Luckily for you, you don't need the old versions. I tested it for you, and you can run the script with the current versions of the dependencies.

Second, you try to install the script as a package (with pip3 install .). This tells pip (or pip3, in your case) to look for an installable package in the current work folder and add it to your python installation. That wouldn't work, as this script is not a package. I hope this is clear now :)

How to run your script? Try the following commands:

  1. pip3 install numpy
  2. pip3 install matplotlib
  3. python3 main.py -> this must be executed from your working folder (e.g. where main.py lives, otherwise you can do python3 /full/path/to/main.py

One more general note

Your python installation can become 'dirty' with all kind of packages installed. That leads to situations that some script don't work properly, although you have installed all dependencies correctly. For example, another package that you have installed can interfere with one of the dependencies, causing unintended side effects.

To mitigate this problem, Python has the concept of 'virtual environments'. This basically makes a clean copy of your Python installation (without user installed packages) for you to work in. This is especially handy when working with multiple users on the same project and you want to make sure everybody is running the same Python installation.

This would help you in the future as well, so I am sharing it just in case (although it is a bit off topic from your original question).

To create a virtual environment, we can use the following commands; python3 -m venv venv This creates a folder venv in your current folder, with its own Python installation (including it's own pip!). However, when using python3 main.py, the python that is invoked is still your 'global' python installation. To start using your new 'virtual' python installation, you need to activate it: source venv\bin\activate You will see that your command line will be prepended with (venv) - and now you will use a clean python installation when running python file.py. If you use pip install numpy now, it will install it into the virtual environment, not in your global python3 installation.

EDIT: one more thing that is MacOS specific; you have python and python3 available to you (globally), the first will refer to python2 and should not be used. However, if you make a virtual environment, you can use either and both are pointing to the virtual python installation.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I install a script to run anywhere from the command line?

How do I run a python script in parallel with different arguments in linux?

How do I run custom python script in Google App engine

How do I run a python script at specific time in a day automatically?

How do I run a python script and pass arguments to it in C

How do I run a local Python script on a remote Spark cluster?

How do I run a Python script on my web server?

How do I run a Python script on my web server?

How do I run another script in Python without waiting for it to finish?

How do I run a Python script from C#?

How do I run a Python script from C#?

How do I run a Python script in the background and restart it after a crash?

How do I run a python script whenever a file is created in a directory?

How do I run a python script using an already running blender?

How do I run the main script of a Python package directly?

How do i run python script on all files in a directory?

How do I schedule a Python script to run in a Linux VM?

How do I run a python script on boot on Google Coral?

How do I run a PowerShell script with parameters from Python

How/where do I upload a (python) script to jenkins to run?

How do I connect a Python script to run combinations on a webpage input?

How do I run a function from another script in Python

How do i run a Python Script on Startup? (Raspberry Pi)

How do I install this script as root in terminal: ./qt-opensource-linux-x64-5.3.2.run?

How do I run qmake to install templar?

How do I install and run a TFTP server?

How do I run this .sh script?

How do I run script with admin perm?

xscreensaver: How do I run a script on unlock?