I have a very basic question: If we want to run a script called script.py, we go to shell and type "python script.py". However, if we want to check, for example, if Django is installed or not, we first go into Python interpreter by typing "python" in the shell, and while we get the >>> then we type import Django. What is the conceptual difference? For example, in the second case, why directly running "python import Django" in the shell does not work?
python import Django tries to run a Python script named import with an argument Django.
python -c 'import Django' would attempt to execute the Python statement import Django as if you had typed it from the Python interpreter directly.
python launch interpreter. You can easly test script on it, and after create a file *.py that you can use on CRON (for ex)
When you type python and import Django it do
Open Python interpreter
Import Django library on it (to use it)
If an error raise, it seems than Django wasn't installed on computer
Related
I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell
I am studying python, both 3. and 2. I started a few days ago.
I want to know the differences between site module and interpreter.
I got this question from
Python exit commands - why so many and when should each be used?
Those explanations are very clear but it's still hard to me.
If I am understanding your question correctly,site is a module in Python. A module is a file containing Python definitions and statements. In order to use the functions (for ex: exit() or quit(), you need to import the site module as those respective functions are defined in there.
The Python interpreter is the program that reads and executes Python code. This includes source code, pre-compiled code, scripts - in this case you reference, you would need to import the site module into your current Python interpreter session, in order to use say exit() or quit() in that given session.
So, the process of this particular question would be:
* Activate the Python interpreter by typing into your respective terminal the version of Python you have installed on your computer, ex. python3.
* In the Python interpreter, type import site
Hope that helps Hwan.
I assume you are stuck on understanding:
"Nevertheless, quit should not be used in production code. This is
because it only works if the site module is loaded. Instead, this
function should only be used in the interpreter."
Basically, what that is saying is that quit is a part of a module loaded in the python interpreter. That module's name, is site.
Firstly, the python interpreter is what you use to run python scripts or environments. It interprets python commands. For example, if you write a = 1 in a script or python environment, the interpreter takes that command and executes it without compiling it. (If it was a language like c you would need to compile the script before you run it).
Secondly, a module is a pre-written file that can define functions, classes and variables. When you write import numpy into python, you are importing the module, numpy. Therefore when they say "this only works if the site module is loaded", that means that import site must be executed.
When you start a python interpreter (by typing python into your command shell), it automatically imports site, which has sys, venv and main etc. which are all required to run an active interpreter session.
I am new to Python and I am wondering how can I keep the modules and data from a script I run from the Python shell?
For example: I have the script helloworld.py and it contains:
import numpy as donkey
a = 55
Then I want to run that script from my Python shell:
execfile('helloworld.py')
However, if I then try to call for 'a' or 'donkey', they are not found.
How can I fix that?
I will assume you are using IDLE. IDLE is not a shell in Unix sense. So there is not a direct equivalent of the bash shell . ~/.bash_profile.
Instead I edit the file and then use F5 Run Module. All my variables, functions, and classes get loaded. I can use the interpretive shell to manipulate. However, if I run another module, I reset the environment, losing the first module's definitions.
The execfile() approach is limited to Python 2 as noted here:
How to run a Python script from IDLE command line?
I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell
When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.