Variable problem on ipython console of python 3 in spyder - python-3.x

Recently I'm considering using mainly python 3 although I have used python 2.7 so far.
But I encountered variable problem on python 3 as follows.
For example, the below code properly works in python 2.
#print a
a=1
I run the code named test.py many times on ipython console (python 2.7.16) in spyder 3.3.6.
After the first run, I remove # in the first line.
Then, ipython console outputs 1 which is a's value.
However, when I run the above code (the first line is replaced by #print(a)) similar to the above on ipython console (python 3.7.6) in spyder 4.0.1, ipython console outputs an error message,
NameError: name 'a' is not defined.
When I input a in the ipython console, the console outputs 1.
Can I do the same thing in my python 3 environment as I do in python 2?
Thank you in advance.

There is a setting when running the script called "Run in console's namespace instead of an empty one".
If you tick that box it keeps the variables in the namespace.

Related

VScode CodeRunner extension error in printing values in same line in python

I am getting this error while running a simple code using Visual Studio Code's CodeRunner extension while print values in the same line separated using a space in python.
for i in range(0,6):
print(i, end = ' ')
Error: print(i, end = ' ')
SyntaxError: invalid syntax.
However, the same code runs I select 'Run Python File in Terminal'
Please help me out on this issue and suggest possible alternatives/fixes.
The reason you are getting SyntaxError has to do with your python terminal version being different than the one you've told vscode to use and therefore the one coderunner uses. You can change the vscode python interpreter (version) by using CTRL+SHIFT+P in vscode. This will bring up the Command Pallet where you can type in python select interpreter and select the version of python that matches your terminal version.
If you don't know which version of python your terminal version is that is running the code correctly, go to the terminal and type python --version

Still running python 2.7 when running python in windows 10 command prompt

When I'm running Flask it runs python 2.7, I want it to run on python 3.6.
When I type into the command prompt:
python
it shows it is running python 2.7
So when I type into the command prompt:
echo %PATH%
it shows that python 3.6 is on the top of the variable list.
SO when the path to 3.6 is on top of the list it should automatically run flask in python 3.6 right?
Your PATH is misconfigured, it should only contains path to folders containing binaries, not path to the binary itself
You should change C:\Python36-32\python.exe to C:\Python36-32

File path wrong in PyCharm Python Console

I'm a beginner of Python and I want to run a python3 script in PyCharm Python Console, but it raise an error below:
>>> /Users/mymac/Documents/Python/test.py
File "<input>", line1
/Users/mymac/Documents/Python/test.py
^
SyntaxError: invalid syntax
I don't know what's wrong the file path is, how can I solve the problem?
use execfile('/Users/mymac/Documents/Python/test.py'). You are trying to run a python file like an executable. On top of that you are doing it in python interpreter.
Use python's execfile routine to call a file from interpreter or use python test.py to run it from the terminal.
I recognized that the answer I accepted ahead wasn't a perfect method, cause function execfile has been deleted in Python3, the alternative method is using open like:
>>> with open ('/Users/mymac/Documents/Python/test.py', 'r') as f:
... exec(f.read())
...
Still thanks for guys who helped me!
This could be several things. Have you set your environmental correctly? You can test this by cmd: python it should return Python 3.6.4 if that is your current version. If not then head over to tutorialsPoint for how to correctly set up your path.
If that is correctly configured, then have you selected an interpreter in PyCharm. If not, File --> Settings --> Project: Network --> Project Interpreter. Select your python installation path.
Another thing to note is that I suspect you mean to use the terminal instead of the python console.
Then in PyCharms terminal section, python test.py.

IPython startup script: Check if QtConsole

For my IPython QtConsole, I have a startup script in my profile_default/startup folder, which contains the following lines:
ipy = get_ipython()
try:
plot
except NameError: # not loaded yet
ipy.run_line_magic("pylab", "inline")
This works fine in the QtConsole and Notebook, but if I now run ipython with no subcommand, i.e. in the Windows command line, it prints an error that the 'inline' GUI is invalid.
Is there any way I can check for the subcommand (qtconsole, notebook or "None") inside the startup script, so I can avoid that message?
(IPython QtConsole 3.2.0, WinPython-64bit-3.4.3.4)
I'm going to assume that ipy is the result of get_ipython().
Warning: You should not try to have code that behave differently on various frontend. It will break at some point, and lead to hard to debug issues that don't make sens. Nasal demons will be on a the lookout for the smallest misstep to haunt you.
That being say, in the pure, classical terminal IPython you can can verify that:
In [1]: type(get_ipython())
Out[1]: IPython.terminal.interactiveshell.TerminalInteractiveShell
Which is not true for Notebook and QtConsole (which are ZMQInteractiveShell). In both case IPython.terminal.interactiveshell.TerminalInteractiveShell should be importable, and you can check with issubclass in which case you are.
Now, you can also create your own aliases on windows (not sure how), that pass extra command line arguments to IPython, in order for notebook and qtconsole to not have the same startup sequence.

Activating a new Conda env through shell from within Jupyter Notebook

I'm working with a Jupyter Notebook written in Python 3, and I would like to run Python 2 scripts from within that Notebook. I was wondering if it's possible to run Shell commands from within the Notebook, and have these Shell commands running under a different environment.
For example, if env2 is a Conda environment that runs Python 2, and env3 runs Python 3, and my Jupyter Notebook runs in env3, maybe I could write within my Notebook:
! source activate env2
! script_that_uses_python2.py
and then continue with python 3 code that is in the Notebook (and uses the output of script_that_uses_python2.py).
I tried it and it didn't work (! conda info --envs showed that env3 is still running). Any advice on how to change the environment in the middle of the Notebook and then go back to the original environment?
As far as I know, you cannot activate another environment and have it work like that. What you can do is run that Python explicitly, something like
!/path/to/anaconda/envs/python2env/bin/python script_that_uses_python2.py
If I run
!/path/to/anaconda/envs/python2env/bin/python -c "import sys; print sys.path"
on my system, it only shows Python 2 directories, so it would probably find the correct imports. However, the variables from that script won't be available in your notebook. You could have the Python 2 file write out a Pickle file and try to read that, maybe...
This worked for me:
! source /home/ubuntu/miniconda/etc/profile.d/conda.sh && conda activate envname && python run.py
Note: It only works if you run all commands in one line connecting them with &&

Resources