I am creating a script that will call an API and return some results. I have the script working with pycharm on my computer but I am running into a few problems but I want to focus on this problem first.
1) I am unable to set Python3 as my default python.
I am using a Mac. When I go into terminal I enter $ python --version and it returns Python 2.7.10
I then enter $ alias python=python3, and when I run $python --version it returns Python 3.7.2
When I create a py.script with the os module, it does not work. See my code below.
import os
os.system('alias python=python3')
print(os.system('python --version')
It prints 2.7.10
I also tried to run the os.system('alias python="python3"')
On -nix machines (including OSX), one way to change the version of the interpreter that the script runs with is to add a shebang as the first line of your script.
Eg.
#! /usr/bin/env python3
import sys
print(sys.version)
Then to run your script do:
~/$ chmod u+x myscript.py
~/$ ./myscript.py
You only need to run the chmod command the first time. It enables you to execute the file. Whenever you run your script directly (rather than as an argument to python) your script will be run using the version specified by the shebang.
welcome to SO! Pycharm needs you to specify which interpreter to use as default, as it wouldn't choose the system one by default.
So if you want python3, you can run which python3, and use the path as a settings for the current project. How to do that step by step is here:
https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
Hope it help, post a comment if you need more details.
This isn't surprising, because os.system opens its own shell, and running alias in that way only affects the currently running terminal. Each call to os.system would be in a separate shell.
I'm not sure what your ultimate goal is, but you almost certainly don't need to change what python means to a shell to do it. If you DO, you'll have to run both commands at once.
import subprocess
cp = subprocess.run("alias python=python3 && /path/to/script")
Interesting - apparently os.system ignores the alias? Just checked it in Linux and got the same results.
Try sys instead of os:
import sys
print(sys.version)
Related
I've installed the cryptography module using pip:
pip install cryptography
Running a simple test.py file with just an import statement, I can confirm that the module is installed. From terminal:
TEST.PY FILE (just one line):
from cryptography.fernet import Fernet
RUNNING FROM TERMINAL:
/usr/local/bin/python3.9 test.py
But when I try to run this script from an Apple Script application, the module cannot be found even though I'm using the same version of python:
RUNNING FROM APPLE SCRIPT:
do shell script "/usr/local/bin/python3.9 test.py"
Any advice would be greatly appreciated. Thanks.
do shell script does not set the working directory so either set it at the start of the shell script:
do shell script "cd \"$HOME\" && /usr/local/bin/python3.9 test.py"
or supply the full path to the test.py script:
do shell script "/usr/local/bin/python3.9 " & quoted form of "/Users/foo/test.py"
I think the difference in execution between Terminal and do shell script is due to the fact that they use different shells. In order for the modules to be found, the $PATH environment variable must contain the path to the /Library/Frameworks/Python.framework/Versions/3.9/bin directory.
You can check what $PATH contains by running the echo $PATH command. At least for me, this variable in Terminal contains the path /Library/Frameworks/Python.framework/Versions/3.9/bin, but in do shell script it does not. One solution might be to add this path to the $PATH. Note that this addition will be persistent only within the current do shell script command.
I haven't tested, try it yourself. Just in case, I also replaced the path to python3.9 with the real one (instead of an alias):
set pathEnvironmentVariable to do shell script "echo $PATH"
if pathEnvironmentVariable does not contain "/Library/Frameworks/Python.framework/Versions/3.9/bin:" then
do shell script "
export PATH=/Library/Frameworks/Python.framework/Versions/3.9/bin:$PATH;
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9 " & quoted form of "/Users/foo/test.py"
end if
Go into your Terminal and start your normal Python. Then import your package and see where it is being imported from:
import cryptography
import sys
# Which Python am I running?
print(sys.executable)
# Where is it looking for modules?
print(sys.path)
# Where was cryptography module found?
print(cryptography.__file__)
Now you know how the proper import works.
Then go into the environment it doesn't work, i.e. Applescript and run a script that shows:
which Python it is running, and
where it is importing from
import sys
# Which Python am I using?
print(sys.executable)
# Where does this Python look for modules
print(sys.path)
I have this file.py:
import os
os.system("pip install pip")
os.system("pip install selenium")
How do I make it work for MAC and what is te equivallent of a .bat file in MAC to execute the file.py.
Your file.py script will generally work fine on Mac as long as the environment the script is running in is set up right. Most notably, the pip executable has to be findable via the current PATH variable. You might benefit by looking at the subprocess module, which is an alternative API for running external commands. It is a more robust mechanism for doing so.
The equivalent of a .BAT file is a shell script. You have a choice as to which shell to use to run the script. I think the most common source is the Bash shell. It is often the case that you use whatever shell is running at your command prompt. This functionality is generally much more general and flexible than a .BAT file is on Window. See this link for a discussion of many of the issues:
https://developer.apple.com/library/archive/documentation/OpenSource/Conceptual/ShellScripting/shell_scripts/shell_scripts.html
A shell script can just be one or more commands that you might run in your Terminal. For example, to run test.py at a Terminal prompt, you'd do this:
> python test.py
The simplest equivalent in a shell script would be the same thing:
python test.py
A script that looks like this is run by whatever shell executes the shell script. What is more usually done is that a "shebang" line is added to the top of the shell script to explicitly define which shell will be used to run the script. So what the single line script above should really look like is this:
#!/bin/sh
python test.py
This may be starting to make your head spin. I would suggest reviewing the link I gave above, and possibly reviewing some other materials that explain shell scripts. Note that nothing about shell scripts is unique to the Mac. The concept is exactly the same on Linux, Unix, etc.
BTW, do you really want pip install pip? What does that do? Doesn't the pip package have to already be installed if the pip command is working?
I work on an api that run python3 scripts like python3 <scriptname.py>. This api initially run on a linux system. Unfortunately I make the local development of it on a windows. Is there a way to simulate this behaviour so that when I run in powershell python3 -V it give me the version of python.
I have allready python 3 installed on my computer. I tried to add the path to my python.exe to the system variables with for variable the string python3. But after registering and restarting powershell and type python3 -V I still get the error that «python3» is not recognized as a command applet name.
Does anyone have any ideas?
Once python is in your path, you can define a PowerShell alias for python3.
PS > New-Alias python3 python
PS > python3 -V
python 3.6.5
In order to have this alias as a permanent one, refer to this question: Permanent PowerShell Aliases.
I found tricky solution.
Once python is in your path, go to python installed path and copy python.exe and named duplicated one as python3.exe
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
I have installed Anaconda 2 & 3 in my system. Anaconda 2 contains python 2.7 & Anaconda 3 contains python 3.6.
I need to run my python code using command prompt & I need to use python 3.6
While I'm running python --version, I'm getting python 2.7.14. How do I change it to python 3.6?
As you can see, I have both Python2 and Python3 installed.
I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM.
Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.
How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.
.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.
Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.
python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.
python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.
I hope this solves your problem.
You should have python3 also in your path. You could use python3 to run your script:
python3 <your_script>
But to answer your question, you could use alias and update it as python.
$ python --version
Python 2.7.6
$ alias python=python3
$ python --version
Python 3.6.4
If you want to run Python 3.6, then execute python3.6.
Otherwise, check with which python where the symbolic link to the actual python executable is. On my system, it's
/usr/bin/python
and when I ls -la /usr/bin/python, it gives
lrwxrwxrwx 42 user user 42 Aug 24 03:45 /usr/bin/python -> python2.7
If you are really sure that you want to do it, you can now just move the old symlink somewhere:
sudo mv /usr/bin/python /usr/bin/old_python
and create a new symlink:
sudo ln -s /usr/bin/python3.6 /usr/bin/python
Now you should get something like:
$ python --version
Python 3.6.5
I don't guarantee that it doesn't break everything in the world, especially if you have some distro that has a package manager that relies on a specific python version under python.
This seemed to work nicely on Linux Mint 18.
I made a small trick to solve this problem. For me
C:\Python27
and
C:\Python27\Scripts
were at the top of the PATH Environment variable list. I simply selected them and clicked on Moved Down to shift them to the end of the list.
Just make sure that your path of Python3 and its Script folder is above Python2 path.
And it solved the problem for me. Hope it would help you too.
Reason :
How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM. Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.
reason is taken from the accepted answer to this question.
In order to run your script with python 3 you can use
python3 <path to file>
P.S.: this should be a comment, but I do not have the required reputation. :)
If Anaconda 3's is the only python3 interpreter in the system you can run python3 instead of python in your command line and it should work.
You can alter PATH parameter inverting the positions of both interpreters.
I have two versions of the python which the first path is python 2(I am still working with python 2), but there are some scripts which should use python 3. This is what I have done:
I have create a bat file.
ex:
open Notepad++ and write :
#echo off
set PYTHONPATH=
"%~dpn0.py" %*
save it as 'yourscriptname.cmd'.
before my script(which should use python3) I just run this file in command line and then run my script. it is working.