Different directory using python3.8 instead of python3.7 - python

I tried to open a file named (choony.py) from the terminal and I got an error.So,
this is what I typed in the terminal:
python3 choony.py
This is the error that I got
No such file or directory
I tried this in the terminal
echo $PATH
This is the result that I got
/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
As you can see, python is running 3.7
how can I make python 3.8 the defult.

Related

ModuleNotFoundError when running BASH script on CMD

I have a BASH script called run2scripts.sh that runs two python scripts at the same time. Here is the BASH script:
#!/bin/bash
python text_reader.py & python text_writer.py &
When I run the bash script on the CMD terminal, the result is:
>bash run2scripts.sh
File "text_reader.py", line 2, in <module>
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
However, when I run the BASH script via Cygwin with $ ./run2scripts.sh, it works perfectly fine. Matplotlib works. I included in my .bashrc file export PATH="PATH TO PYTHON INTERPRETER" so python always goes to the path with all the modules installed. Furthermore, the command where python in CMD returns 2 results. C:\Cygwin64\bin\python and the path of the python interpreter with the modules installed. And CMD uses the latter path. The path to the interpreter is also apart of my PATH environment variable. So for these reasons, CMD shouldn't be returning a ModuleNotFoundError. print(sys.path) includes the path where Matplotlib is installed. The which python command in CMD however returns /usr/bin/python. Why is this happening and how can I fix it? I suspect the problem is related to the which python command and /usr/bin/python.

Anaconda prompt: ModuleNotFoundError when running Python script

I am trying to run the following script "test.py" on Anaconda Prompt:
from tensorflow.keras.applications.resnet50 import ResNet50
...with the following command:
(conda_env) C:\dev>test.py
This results in the following error:
ModulNotFoundError: No module named 'tensorflow'
When I run the same script on Anaconda Prompt with the following command I don't get any errors:
(conda_env) C:\dev>python test.py
I have installed tensorflow in the Anaconda environment 'conda_env'
(conda_env) C:\dev\>conda env list
# conda environments:
#
base C:\Users\xx\Anaconda3
conda_env * C:\Users\xx\Anaconda3\envs\conda_env
keras_1 C:\Users\xx\Anaconda3\envs\keras_1
tf-gpu C:\Users\xx\Anaconda3\envs\tf-gpu
Why is this like that?
You won’t get errors if you do
(conda_env) C:\dev> python test.py
because then you’re following the correct syntax for running python scripts in a terminal. By adding python before the .py file, you initiate the Python interpreter that executes your script. Without it, the terminal won’t know what Python interpreter to use to execute your script and may end up using an interpreter that doesn't have the modules you require. There are ways to skip writing python before executing if that’s what you want.
For example, see: Calling a python script from command line without typing "python" first

Running python2 script in python3 loaded unix environment

my current bash shell is loaded with python3 but occasionally I tend to run old python2 scripts and I get this error
python/2.7.10/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory"
so every-time I had to load python2 back and forth. Is there a way to run python2 script in python3 environment by just changing any environmental variables?
Are you open to virtual envs? Otherwise you can run python2 or python3 from the command line.
As an example, create the following file, hello_world.py:
#!/usr/bin/env python
print "Hello, World!"
Then you can run from the bash shell:
python2 hello_world.py
That should work no problem. However, if you run:
python3 hello_world.py
You will run into the error:
SyntaxError: Missing parentheses in call to 'print'. Did you mean
print("Hello, world!")?

Windows command line, "python3" pointing to the wrong python3.exe

So, I have Anaconda, OSGeo and Python2.7 installed on my computer.
I'm also using Spyder. In Spyder :
>>> import sys
>>> sys.executable
'C:\\ProgramData\\Anaconda3\\pythonw.exe'
Which is what I want.
However, in the windows command line and powershell :
$ python3
>>> import sys
>>> sys.executable
'C:\\Progra~1\\OSGeo4W\\bin\\python3.exe'
Which is not what I want. I want to use 'C:\\ProgramData\\Anaconda3\\pythonw.exe' (or python.exe, not sure) when using python3 in the command line.
Also :
$ pip3
Fatal error in launcher: Unable to create process using '"'
I don't get why python3 in the windows command line points to OSGeo's version of Python3. Here is my path :
C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Anaconda3;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files\PuTTY\;C:\Progra~1\OSGeo4W\bin\;C:\Program Files\Microsoft\R Open\R-3.4.0\bin
I also have an environment variable called PYTHONHOME
C:\ProgramData\Anaconda3
Moreover (for completeness of information), I have python 2 installed :
$ python
File "C:\ProgramData\Anaconda3\lib\site.py", line 177
file=sys.stderr)
^
SyntaxError: invalid syntax
($ pip outputs the same thing).
Having python3 and python2.7 both work when using python3 and python (respectively) in the windows command line would be a nice bonus, but it's not really my priority.
There are probably several things you have to take care of:
In general the search order of the Windows PATH is from left to right starting with the system PATH. The first matching element wins. In your case this is correct because the system will search C:\ProgramData\Anaconda3\ first. However in that folder is no executable called python3 by default. On my system I created a simlink pointing to python.exe. On your system you can do it in PowerShell like this:
New-Item -Path C:\ProgramData\Anaconda3\python3.exe -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\python.exe
pip is located in Scripts\ folder so in your case you have to add C:\ProgramData\Anaconda3\Scripts to your PATH and create the corresponding simlinks again. In this case you have to create two of them because pip.exe is appending its name to the script that is trying to call (i.e. if your exe file is called foo.exe it will try to call foo-script.exe which does not exist) you can create the simlinks in PowerShell with those two commands:
New-Item -Path C:\ProgramData\Anaconda3\Scripts\pip3.exe -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\Scripts\pip.exe
and
New-Item -Path C:\ProgramData\Anaconda3\Scripts\pip3-script.py -ItemType SymbolicLink -Value C:\ProgramData\Anaconda3\Scripts\pip-script.py
Like this you will be able to use python3 and pip3 from your cmd line. Please check for similar problems with your python2 installation folder.
Hope it helps.

SublimeText3 cannot find Python modules (numpy) installed with MacPorts

I installed Python 3.5 using MacPorts. I am trying to use SublimeText3 as an editor. (Anything better and more integrated tan ST3 for python development??)
From the MacOSX terminal, I can 'import numpy' just fine, but SublimeText3 cannot find the packages.
Is it because the python packages are installed as 'Frameworks'?, because the Path for finding those modules is not right?, other????
Here's what terminal shows:
$ type -a python3
python3 is /opt/local/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.5/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.5/bin/python3
python3 is /usr/local/bin/python3
Here's what ST3 shows:
File "/Users/xxx/Desktop/python_work/array_play.py", line 1, in <module>
import numpy
ImportError: No module named 'numpy'
[Finished in 0.0s with exit code 1]
[cmd: ['/Library/Frameworks/Python.framework/Versions/3.5/bin/python3', '-u', '/Users/xxx/Desktop/python_work/array_play.py']]
[dir: /Users/xxx/Desktop/python_work]
[path: /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin::/Library/Frameworks/Python.framework/Versions/3.5/bin/python3/site-packages]
As you can see, I tried to add
/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages
to the Path variable, since this is the location of the numpy folder, but this doesn't work...
Should I just ignore this message in ST3 and use it only as an editor??? Seems rather silly to have an IDE and not be able to build and run programs from it...
First, you need to find out which python3 you are using in terminal. You can do this by running command which python3. Suppose the output is: /Users/username/.anaconda3/bin/python3.
Second, try to set PYTHONPATH environment variable in Sublime's settings. Go to Preferences -> Settings User, and add the following lines at the end of the settings file (but inside of the last } symbol).
"env":
{
"PYTHONPATH":"/Users/username/.anaconda3/bin"
}

Categories

Resources