Don't know how to run a Python script - python

I'm trying to run a Python script, But I don't know where I should run it.

You are in wrong directory. First, change your directory with:
cd AppData\Local\Programs\Python
Then when you are in correct directory you can run:
python discordgenerator.py
What is the cd command?

Related

How can I run Python program on cmd?

I've tried to run code on the cmd but it isn't working. I'm using windows7 and I changed the path from the default but I wrote the directory on the cmd. Can anyone help me?
python3 yourfilename.py
The above command can run your code, makesure you are at the project directory.
make sure python in your system path;
set python root path to system environment variable;then you can run 'python filename.py' ex

How to execute python3 program in shell script

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

Running python in terminal without suffix

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

How do I get linux to automatically run my python script in the Python interpreter?

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.

Cannot get python on mac osx to reference my scripts folder

Am pretty new to python although have programmed a lot before. I'm using mac osx snow leopard and python 2.6.1.
I've followed this post about setting your PYTHONPATH to a custom scripts directory but can't get python to recognise it.
How can I run my python script from the terminal in Mac OS X without having to type the full path?
So i've got a simple helloworld script in /Users/richn/Documents/scripts/ called hello.py
Inside is this
#!/usr/bin/env python
print "Hello World!"
I've created a .profile file in my home directory with this in it
export PYTHONPATH=/Users/richn/Documents/scripts
I've also changed permissions of the file to make it executable with chmod a+x hello.py
Running ./hello.py in the terminal from that scripts folder works fine however whenever i run it outside of that folder i get this error
-bash: ./hello.py: No such file or directory
How can i get my scripts to run outside of that folder? Anyone got any ideas?
Thanks very much
What you'll want to do is to edit your PATH variable which is a list of directories your command shell checks when you run a command that does not begin with / or ./ rather than PYTHONPATH:
export PATH="$PATH:/Users/richn/Documents/scripts"
After you have exported your PATH variable you should be able to confirm that it has exported correctly:
echo $PATH
and afterwards you should be able to run "hello.py" successfully.

Categories