No, a bin directory is not for storing only binary files. It's for keeping executable files, primarily.
Historically, before scripts written in various scripting languages became more common, bin directories would have contained mainly binary (compiled or assembled) non-text files, as opposed to source code. The main thing about the files in bin nowadays is that they are executable.
An executable script is a text file, interpreted by an interpreter. The script in your example is a Python script. When you run it, the python interpreter (which is another executable file somewhere in your $PATH) will be used to run it.
Also, as an aside, a text file is as much a file made up of zeroes and ones as a binary file is.
Answer from Kusalananda on Stack ExchangeCan someone explain to me what is a purpose of a 'bin' folder in a package?
What kind of script would one put into 'bin' folder and how would that script be different from the main package's module?
Traditionally as far as I can tell the bin folder is for scripts you intend the end user to run directly such as django's django-admin.py
Whereas the library/package as a whole is mostly meant to be imported into other scripts.
I don't personally have any bin folders in any of my projects because I tend to only write one off scripts meant to be used directly.
If you're writing an application, you can specify a list of callables (functions/methods) in setup.py, and setuptools will generate an executable to run that callable directly.
See this for more info: https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html
No, a bin directory is not for storing only binary files. It's for keeping executable files, primarily.
Historically, before scripts written in various scripting languages became more common, bin directories would have contained mainly binary (compiled or assembled) non-text files, as opposed to source code. The main thing about the files in bin nowadays is that they are executable.
An executable script is a text file, interpreted by an interpreter. The script in your example is a Python script. When you run it, the python interpreter (which is another executable file somewhere in your $PATH) will be used to run it.
Also, as an aside, a text file is as much a file made up of zeroes and ones as a binary file is.
“Binary files” is the etymology of the bin directory, but not its current meaning. Originally, it was for binary files, as in compiled programs — files containing machine code that could be loaded into memory and executed as code. But quickly it came to contain any program that could be executed, whether this program was machine code or a script that is interpreted by some other program. Conversely, files containing machine code that cannot be executed directly is not stored in /bin, but in /lib (for library).
How to import your package/modules from a script in bin folder in python - Stack Overflow
Shell variable to get Python bin directory? - Stack Overflow
terminal - Why isn't a binary not being found in my PATH? - Ask Different
How to package a script and have it install to the bin directory?
I just recently got through Ex46 in LPTHW myself. Like you, I was confused by the scripts. In case the other answer was too advanced for you, I ended up just putting in a simple "hello world" script:
#!/usr/bin/env python
from test3 import printstring
printstring.printstring("test script working")
print "test over"
I named that file testscript3.py (*Note, I learned later that it would be more convenient to leave off the .py filename extension if it were a real script that I wanted to seem like a system command)
My file test3.py was like so:
def printstring(s='you did not provide string'):
print s
Here's some newbie things that I learned while trying to get this process to work:
The #! symbol is sometimes pronounced shebang and the simple explanation is that the command on that line tells the shell to use python to run the script. If you leave off the ".py" filename extension, then the user of the script doesn't need to care what interpreter is needed to run the script. See wikipedia shebang article.
I ran the following command to package the distribution:
python setup.py sdist
After doing that, I was able to install the package and script by running
sudo pip install test3-0.1.tar.gz
One thing I worried about was permissions on the script file. However, I noticed that distutils took care of this when packaging (changed mode to 755 or whatever).
You can find my whole project for this example on github.
For example Django's project creating, Scrapy's project creating, django-admin.py and scrapy are both scripts in bin folder.
You could get even more examples by checking almost python-based tools.
You should be able to create shortcuts and run them like in your original attempt. What's not working? But yes, you would have to name the shortcuts something unique in your bin folder. You would need to include the .lnk extension when executing.
So, in your C:\bin folder and with it in your path create a shortcut to the Python32 exe, name it python37 and execute it by:
C:\somedir> python37.lnk
Alternatively, you could create a .bat file and run it. For Python37 you could create C:\bin\python37.bat with this as the contents:
"C:\Users\Samuel\AppData\Local\Programs\Python\Python37\python.exe" %*
That should let you exclude the extension if you just want a clean python37 command.
C:\somedir> python37
You can simply have both folders in the path and make a copy of your Python37 and 38 with those names. It wouldn't hurt anything to have it executed from python37.exe instead of python.exe.
Usually, setup.py should install the package in a place where the Python interpreter can find it, so after installation import mypackage will work. To facilitate running the scripts in bin right from the development tree, I'd usually simply add a simlink to ../mypackage/ to the bin directory. Of course, this requires a filesystem supporting symlinks…
I'm not sure if there is a "best choice", but the following is my normal practice:
Put whatever script I wanna run in /bin
do "python -m bin.script" in the dir myproject
When importing in script.py, consider the dir in which script.py is sitting as root. So
from ..mypackage import core
If the system supports symlink, it's a better choice.
Consider using console_scripts:
from setuptools import setup
setup(name='some-name',
...
entry_points = {
'console_scripts': [
'command-name = package.module:main_func_name',
],
},
)
Where main_func_name is a main function in your main module.
command-name is a name under which it will be saved in /usr/local/bin/ (usually)
The Python documentation explains it under the installing scripts section.
Scripts are files containing Python source code, intended to be started from the command line.
setup(...,
scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
)
As mentioned here, beside scripts, there is an entry_points mechanism, which is more cross-platform.
With entry_points you connect a command line tool name with a function of your choice, whereas scripts could point to any file (e.g. a shell script).
You need to run the program with escalated privileges. Under Ubuntu, this is normally done with the sudo command, which will prompt the user for their password.
You can't just escalate the privileges of alredy running script (well, unless you use some local exploit).
The best way to go is to follow @Lattyware's advice and just force user to run the script with root priveleges (via sudo or by any other means). This is pretty common practice for installers.
However, if you REALLY need to escalate privileges midway (I can't imagine why), you can do something like:
import os
cpstr = 'echo %(pass)s | sudo -S cp "%(from)s" "%(to)s"'
os.system(cpstr % {'pass':'userpassword', 'from':'./build/bin/myapp', 'to':'/bin/myapp'})