I´m trying to execute locally installed programs from a Python script (OSX), but they are not found, since /usr/local/bin is not in the PATH. Running os.environ gives only /usr/bin:/bin:/usr/sbin:/sbin.
It is probably a common/simple problem, but I´ve exhausted Google, and start feeling a little stupid :-)
How do you try to execute your programs (please provide us a minimal code sample)?
If you are using the subprocess package, you can try to provide the full path of your executable:
subprocess.run(["/usr/local/bin/my_program"], ...)
Else, you can try to append /usr/local/bin to the os.environ list.
Related
During zerphyr rtos installation process i ran into this error https://pastebin.com/8F5A8S8m, which i safely brute-forced by the link given in the next sentence, but now same error but different file. And here is the code from that file https://pastebin.com/EawPKXRG. So, in short, by googling i couldn't find a way to change interpreter that python uses in this particular files.
FYI
which python
gives python 3.11 one
sudo su
nano etc/path
gives also right directory for python 3.11, but somehow these files use python 3.9 which is installed on this mac by default.'
p.s sorry for the grammar and structure, I'm so mentally drained now to write it properly.
Didn't find anything on google
So you need to run those python files in this format
python3.11 your_desired_file_name
I'm having problems running my python script on the cmd line and am running into a module error (1st screenshot)
What I done so far was go into path environments. After that I created a variable, under system variables, that contains the below (2nd screenshot).
I then created a variable called: %PYTHON_HOME% in the variable called Path (also under system variables).
So far I have no problems running the script in Atom but if I were to run this in cmd prompt I can't get it to run. Would anyone have any advice on what else I could do?
Some additional things I had done:
I entered the below in Atom to see my sys.path.
import sys
for i in sys.path:
print(i)
Output
C:\Users\JIMMY-PC\Desktop\PythonNotes
C:\Users\JIMMY-PC\Desktop\Python Notes
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39
C:\WINDOWS\system32
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\python39.zip
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\DLLs
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\win32
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\win32\lib
C:\Users\JIMMY-PC\AppData\Local\Programs\Python\Python39\lib\site-packages\Pythonwin
UPDATE2:
I tried running my python script in cmd line. Looks like what's appearing in Atom and comparing to cmd line contains different python version. Not sure how to fix this so it looks at python 3.9 instead of 3.7. Any advice would be much appreciated!
Found the answer! Will leave my solution here in case other people have this problem....
Because I have different version of python in my computer, that was the reason my computer was having trouble finding the correct module path. I decided to go over to control panel>programs>uninstall a program> all python versions except for the latest version.
As for path environments I created PYTHON_HOME under system variables with the two paths inside (screenshot below). Then I plugged in %PYTHON_HOME% inside Path, restarted my comp and now I can run my python scripts from both my Atom IDE and cmd prompt!
I feel a little foolish that I don't know this, but I tried to do it today and was surprised when it didn't work....
I have a directory C:\test with a demo script, lets call it demo.py
If i am in C:\test then I can just do python demo.py. Easy
I could also use a relative path, so from C:\, it's python test\demo.py
What if C:\test is on the path?
I was expecting to be able to now do python demo.py from anywhere however...
python: can't open file 'demo.py': [Errno 2] No such file or directory
I feel foolish because I thought this was straightforward, but I have searched around and have not found a solution. Am I fundamentally misunderstanding something here about how the Python interpreter finds scripts to run? I don't think this is anything to do with PYTHONPATH, as I understood that to relate to loading of modules inside scripts.
This is on Windows 7, by the way.
The PATH is only used to search for commands. A first way is that a Python script can be used directly as a command and in that case the PATH will be used: just use demo.py instead of python demo.py.
It will rely on OS specific ways. On Windows, file type (given by the extension - here .py) can be given default application to process them, while on Unix-like, the first line of a script can declare the program that will process it.
Alternatively, python allows to launch a module that will be searched in the PYTHONPATH (not PATH) by using python -m module or for Windows py -m module.
When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)
Here is the problem I am trying to solve. I don't have a specific question in the title because I don't even know what I need.
We have an ancient Hadoop computing cluster with a very old version of Python installed. What we have done is installed a new version (2.7.9) to a local directory (that we have perms on) visible to the entire cluster, and have a virtualenv with the packages we need. Let's call this path /n/2.7.9/venv/
We are using Hadoopy to distribute Python jobs on the cluster. Hadoopy distributes the python code (the mappers and reducers) to the cluster, which are assumed to be executable and come with a shebang, but it doesn't do anything like activate a virtualenv.
If I hardcode the shebang in the .py files to /n/2.7.9/venv/, everything works. But I want to put the .py files in a library; these files should have some generic shebang like #!/usr/bin/env python. But I tried this and it does not work, because at runtime the virtualenv is not "activated" by the script and therefore it bombs with import errors.
So if anyone has any ideas on how to solve this problem I would be grateful. Essentially I want #!/usr/bin/env python to resolve to /n/2.7.9/venv/ without /n/2.7.9/venv/ being active, or some other solution where I cannot hardcode the shebang.
Currently I am solving this problem by having a run function in the library, and putting a wrapper around this function in the main code (that calls the library) with the hardcoded shebang in it. This is less offensive because the hardcoded shebang makes sense in the main code, but it is still messy because I have to have an executable wrapper file around every function I want to run from the library.
I would change the environment variable PYTHONPATH and also the environment variable PATH. Point PYTHONPATH to your virtual environment and PATH to the directory that contains your new python executable, and make sure the path to your python executable comes first.
I accepted John Schmitt's answer because it led me to the solution. However, I am posting what I actually did, because it might be useful for other Hadoopy users.
What I actually did was :
args['cmdenvs'] = ['export VIRTUAL_ENV=/n/2.7.9/ourvenv','export PYTHONPATH=/n/2.7.9/ourvenv', 'export PATH=/n/2.7.9/ourvenv/bin:$PATH']
and passed args into Hadoopy's launch function. In the executable .py files, I put the generic #!/usr/bin/env python shebang.