I feel like this is incredibly easy to fix, but for some reason it isn't.
I want to run a program in linux that opens python file filename.py by writing:
python3 filename arg
but it only works if i write:
python3 filename.py arg
Is there an easy way to run it without adding the extension? And without removing the extension completely? I wouldn't have imagined this to be a problem at all, but here we are.
Thankful for help!
Your file is called filename.py, therefore you call it with python3 filename.py. If you want to call it with python3 filename, you'll need to rename the file.
The one thing you can do is call Python with the -m switch, which will try to import a module with that name, i.e. python3 -m filename. That should work without renaming the file.
Firstly, if you start your python script with the shebang, then you won't need to type 'python3' before the filename each time you wish to run the code.
`#!/usr/bin/env python3'
Secondly, if you create a setup.py file is the same directory. Then you can install your script locally using:
$ sudo pip3 install -e .
from within your directory.
You will then be able to run your script from anywhere within terminal using:
$ myscript args
More info on creating a setup file here: https://python-packaging-user-guide.readthedocs.io/tutorials/distributing-packages/#setup-py
and example setup file here: https://github.com/pypa/sampleproject/blob/master/setup.py
Related
This question has been asked before, but the answers are all several years old and I could not get any to work for me, so I would appreciate some help.
The question is simple, really: I have a python script, and a virtual environment I want it to run in when I double-click it, or call it from another program. How can I achieve this?
you must add packages directory address to your sys.path
sys.path a built-in variable within the sys module. It contains a list of directories that the interpreter will search in for the required module.
add line below to top of your script, now you can double click on script file and every thing must work fine
import sys; sys.path.append("./<environment path>/Lib/site-packages")
this is simple solution however, as sinoroc said it's not good solution because in this situation you are using system interpreter not virtual environment interpreter so i wouldn't be surprised if things don't work as expected.
also you can download all packages that you need and extract them into single folder and add that folder to sys.path
first download packages you need with command below
pip download <package names> --dest <directory name>
for example:
pip download requests --dest packages
at the end add folder contain you packages to your path
import sys; sys.path.append("./<packages directory>")
Recommended Solution
the best solution is that write little batch script for windows or shell script for linux that automatically active virtual environment and then run your script
first create file with .bat extention for windows or .sh extension for linux and then add line below to it
for .bat file
<environment path>\Scripts\activate && python <script name>.py
for .sh file
source <environment path>/bin/activate && python <script name>.py
now you can click on this batch script *.bat file or shell script *.sh file and everything work's fine
Some ideas
Shebang
Place a shebang pointing to the Python interpreter inside the virtual environement with the full path at the top of the script.
#!/path/to/my/venv/bin/python
import sys
print(sys.executable)
Shell/batch script
Write a shell script wrapping the call to the actual Python script:
#!/usr/bin/env sh
/path/to/my/venv/bin/python /path/to/my/script.py
Python wrapper
#!/usr/bin/env python3
import subprocess
command = [
'/path/to/my/venv/bin/python',
'/path/to/my/script.py',
]
subprocesss.check_call(command)
I created a python script ExeMain.py that use virtualenv for dependence. So to launch this script using .desktop I made this command
Exec=sh -c 'source ~/PycharmProjects/ProBlog/venv/bin/activate; python ~/PycharmProjects/ProBlog/ExeMain.py;'
Which resulted with this error "sh: 1: source: not found" and followed by "ImportError"
Without the source command in .desktop file with all necessary modules installed in direct python environment it works fine (in other Linux machine). As it seams not to be a good practice i tried using venv. And as of the errors i could see than the import error is because source command is not executed.
When i launch the .desktop file it should execute the ExeMain.py file.
Standard versions of sh does not have the source command. Perhaps use /bin/bash instead? So it'd be written something along the lines of this:
Exec=/bin/bash -c 'source /home/username/PycharmProjects/ProBlog/venv/bin/activate && /home/username/PycharmProjects/ProBlog/venv/bin/python /home/username/PycharmProjects/ProBlog/ExeMain.py'
I also suggest three other changes: 1) the && operator so that it won't try to run python code until the activate is finished, 2) replace the ~ tilde with the full path in case the desktop environment doesn't properly expand it, and 3) put in the full path to the venv python so that it doesn't default to the system python. Some of these may not be necessary (depending on the system setup), but it doesn't hurt to be careful.
I have a python program which I need to run at a particular day of a month, so I am using crontab for this task and create a shell script to run this python program.
This is part of my shell script:
#!/bin/bash
filepath='file2018'
cd ${filepath}
python3 file.py
When I run the crontab which executes the shell script, the log file shows the following error:
line 9: python3: command not found
I'm really confused about why this error occurs because I have already install python3 and I can run python3 directly from the command line.
Besides, if I replace python3 with python, the shell script works! My python version is python2, but I have to use python3 for this program, so I have to use python3 instead of python.
My operating system is Linux CentOS.
Hope someone can give me some tips!
You can give the full path to the python3 executable. You can get it using the which python3 command. Try it out.
in file.py add first line like below and add +x permission to file.py file
#!/usr/bin/python3
it will automatically execute, no need to mention python3 in the script
use "which python3" command to know exact path of python3 in your machine
#!/usr/bin/env python
I put that at the top of a script. I've seen that should make the script runnable from the command line without the need for python programname.py. Unless I'm misunderstanding I should be able to use programname.py as long as I have the above line at the top of the script. Is this correct?
It isn't working for me I just get an error indicating that I would have to use python at the beginning of the 'call'.
Universal running of Python scripts
You can pretty much universally run without the shebang (#!) with
python myscript.py
Or nearly equivalently (it places the current directory on your path and executes the module named myscript) (preferably do this!):
python -m myscript
from the command line, as long as you have Python installed and on your path environment variable (i.e. set to run with python, which, if installed, would typically be the case).
Shebangs (#!) are a Unix thing.
The shebang, as you're using it, is typically for running on a Unix platform (typically Apple or Linux). Windows would typically require cygwin to use the shebang.
You can usually default to whatever python is available on your system path with:
#!/usr/bin/env python
Assuming you're on a Unix, you might try other locations for your python setup, like:
#!/usr/bin/python
Muddling through
You can see what python you're currently using by using the unix which command, so if you want to see where your python is coming from, use this command:
which python
or on Windows (cygwin probably can run the shebang):
where python
On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod
chmod +x myscript.py
(chmod also may apply to Cygwin in Windows)
If you're not running as root, you may require sudo, and that would be
sudo chmod +x myscript.py
And then attempt to run (within the same directory) with
./myscript.py
make the file executable
sudo chmod +x /path/to/file.py
and then from the same directory as file.py:
./file.py
I've decided that it would be good for me to move outside of my .NET bubble and start experimenting with other technologies. I have Ubuntu12 running and python2.7 and 3.2 are installed. I can run code directly in the interpreters.
I have a basic script on the filesystem called Standalone.py:
#!/usr/bin/env python3.2
import sys
print("this is a standalone script.")
When I'm at my bash prompt I type $ python3.2 Standalone.py. I get a response saying this is a standalone script. But when I type $ Standalone.py then it tells me that the command is not found.
How do I run such scripts?
Thanks for any help.
update
I changed the permissions of Standalone.py to 755. Then I ran the command:
$ ./Standalone.py
and received the message:
: No such file or directory
I then switched the permissions of Standalone.py back to 644. Then when I ran
$ ./Standalone.py
I received the message
-bash: ./Standalone.py: Permission denied
Is there something I'm missing?
You need to make the script executable using
chmod +x Standalone.py
Usually, the current directory is not searched for executable files, so you need to use
./Standalone.py
to tell the shell that the script is in the current directory.
Make sure your script file has linux newline (just \n) not windows newline (\r\n). Did you write the script on windows? This happened to me once. You should check your editor settings.
Your script should start with #!/usr/bin/python not #!/usr/bin/env python3.2
Make sure you're in the folder where your script is located you can check with ls
chmod +x Standalone.py
./Standalone.py
At first, to excecute a script it need to be executable. So you either have to do a chmod +x $file or a chmod 0740 $file. When you set the file permission to 644 you are putting the execute right away, so if gives you an error. If you are unsure of execution right and octal notation, you can use this : http://permissions-calculator.org/decode/0644/.
To really answer your question then, if you want to call the script with $file.py it needs to be in your PATH variable. You can display it with echo $PATH. Those are the directories that are searched for script to execute. So you simply need to give your script the executable right and put it in one of the directory given by your PATH.
Can you check if /usr/bin/python or /usr/bin/python3.2 exists
Execute below comamnd:
which python3.2
and then use the resulting path on top of you script.