Python Executable Crashes in Conda Environment - python

Let's say I have two files we'll call test1.py and test2.py, and I want to run both of these files as executables. I'm familiar with the standard procedure of adding a shebang followed by the path to the desired python interpreter and then running chmod u="rwx" file.py.
I also know that when using conda, each environment gets its own unique interpreter with which to run scripts. So naturally, I activate my environment, run which python and add that command's output to my script like so...
test1.py
#!/home/my_name/anaconda3/envs/env_name/bin/python
print("foo")
Which when I run it as ./test1.py gives me the following error...
./test1.py: line 2: syntax error near unexpected token `"foo"'
./test1.py: line 2: `print("foo")'
However simply running python test1.py gives...
foo
Now let's say I return to my base environment and following the same procedure as above, I create the following script...
test2.py
#!/home/my_name/anaconda3/bin/python
print("foo")
This script runs without error and gives the correct output regardless of whether or not I run it as an executable.
What do I need to do in order to run my python scripts without these errors?
EDIT
Running which python in env_name gives
/home/my_name/anaconda3/envs/env_name/bin/python
Whereas running the same command in base gives
/home/my_name/anaconda3/bin/python

I had the same issue when line 1 was empty, and the interpreter was set in line 2. This results in bash assuming that it's a bash script, and as a result, you get a "syntax error" from trying to execute python commands in bash.

Related

A python problem in running the command to execute a python file in vs code

I was trying to run a python file "exe.py" on vs code by typing:
python3 .\exe.py
But it comes with the following replys from vs code:
python3: can't open file '/Users/exercise/my-python-app/myenv/.exe.py': [Errno 2] No such file or directory
No ideas why couldn't it works given that I already installed the python package and running the command in appropriate environment.
You are running
python3 .\exe.py
which is escaping the e and therefore is replaced by e as there's no matching escape char and it becomes
python3 .exe.py
You need, as other response states
python3 exe.py
or
python3 ./exe.py
In fact python3 is installed in your system but you are typing incorrectly the command. Imagine that you have the python file named exe.py with the following code
print("Hello world")
If you are in the same directory of the file you can execute it using the command
python3 exe.py
Another way to execute exe.py is using the shebang line. The shebang is a line that you can write to tell to the operative system which executable must use to interpret the code of the file. If you add the typical shebang for python3 in linux at the beginning of exe.py
#!/usr/bin/env python3
print("Hello world")
and you tip the command
./exe.py
you will see the result of the execution too. If you get an error probably you have to give permissions to the file with the command
chmod 777 exe.py
You can find a lot of resources talking about the shebang. The following link talks about the shebang in python
https://www.pythonpool.com/python-shebang/
I hope you found it useful.
Greetings.

Bash script to run python script that trigger a c++ executable

I have a c++ code that is compiled and can be executed.
Let's say the output file after compiling is executable.x.
I also have python script that can call this executable and run it.
pythonScript.py:
# lets say the path is absolute for simplicity.
file_path = 'C:/MyProject/code/executable.x'
# I need to pass an argument to main.cpp
subprocess.check_call([file_path, '-switch1'])
I can run the python script from terminal, and it runs the executable without any issue.
Then there is a shell command to run the python script.
myShell.sh
#!/bin/sh
pwd
(cd pythonScriptDirectory && python3 pythonScript.py)
pwd
By running the sh script, it sets the working directory (like how I run python script from terminal), and then it runs the python script. It seems it also finds the executable.x, but it always return with some error.
Is there any suggestion what might be wrong here, or what would be the debugging approach.
The return value specifies the error code 3221225785 in decimal, which is C000 0139 Hex. My assumption is that the executable file can be selected to run, but a working directory issue causes that libraries being used by executable cannot be loaded.
Here is directory structure:

Run terminal script in python?

I am using python and I am trying to run a shell script that is located in another folder I am trying
subprocess.call(['source','../Apps/appName/run'])
Where 'run' is a shell script I wrote and made an executable. But it keeps giving errors such as
No such file or directory or **No such file or directory: "source"
I have also tried the following
subprocess.call(['source','../Apps/appName/run'])
subprocess.call(['source ../Apps/appName/run'])
subprocess.call(['.','../Apps/appName/run'])
I am trying to run the script and leave it alone (as in I do not want any return value or to see what the output of the shell script is.
Thank you in advance
source is a shell builtin that reads a file and interprets the commands in the current shell instance. It's similar to C #include or Python import. You should not be using it to run shell scripts.
The correct way to run a script is to add a shebang like #!/bin/bash to the first line, and chmod +x yourscriptfile. The OS will then consider it a real executable, which can be executed robustly from any context. In Python, you would do:
subprocess.call(['../Apps/appName/run'])
If for whichever reason this is not an option, you can instead explicitly invoke bash on the file, since this is similar to what would happen if you're in bash and type source:
subprocess.call(['bash', '../Apps/appName/run'])

Can't run Python file via Command Prompt, File is not defined error

I am new to Python and currently doing a basic python course to learn. I have been running code all day via the Command Prompt and it has been working fine. For some reason though it has stopped working and python files I try to run are returning the following error:
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
As per the screen shot and the numbers on it, I performed the following steps in an attempt to run the file:
change to the folder where file is saved
run 'dir' to list all files. I am trying to run 'hello.py'. This contains the code: print('hello simon!')
I can run the file by just typing hello.py into the command prompt, this works ok
I can also run with: python hello.py - this works ok
when I activate Python by typing Python --> Enter, this starts the interpreter ok. However if I then try to run by typing hello.py I get the error message.
This has worked ok all day, I have not changed anything on my PC (to the best of my knowledge!) but just started to get this error a few hours ago. I have looked all over the internet for solution but found nothing. I have uninstalled and re-installed Python, restarted etc... all to no avail.
I am running Python 3.6.5 on a Windows 7 64 bit PC.
It won't let me attach a picture so here is link to screenshot of Command Prompt and error: https://i.stack.imgur.com/BBUe5.jpg
I hope someone can help me with this please
Thankyou
You are not supposed to execute hello.py in the Python Interpreter. It won't work. When you type in python and hit Enter in your Command Prompt, Just type this,
>>> print('hello simon!')
And hit Enter, it would definitely work. Because the interpreter is supposed to execute a code line by line. So if you want to run a Python Script then do not execute it in the Interpreter.
The problem is that when you write python (alone), the command line calls python shell and the following commands are run inside the python shell and not in the command line anymore. Calling a script from the shell has a different format (look it up). You can enter exit() to exit the shell back to command line again
What you are trying to achieve is you are running Hello.py inside Python.
Not with Python.
You need to run Hello.py with Python. As python is interpreter over here.
>>>python
means you are going inside python shell
>>>print('hello simon!')
Is equivalent to your program.
You are running your Python Script as you should and it's working. If you added Python to your path you can run Script you only need to call the Script "hello.py". If you have more than one intepreter, or you didn't added it to your path then you can call it like this "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" you can enven pass arguments to it "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" --argument
When you type python in a shell, then interactive mode is activated. This is like a shell where you type commands and got interpreted right away, in the same way as cmd and powershell works, but for python. This way you can test snippets, or just do simple stuff overly complicated like this
import os
ls = os.listdir(os.path.abspath('c:/'))
def print_dir():
for file in ls:
print(file)
Wich in cmd would be dir c:\ or in powershell ls c:\. The point is that you can test libraries, explore objects, replace the shell or just have fun.

File "<stdin>", line 1 in python 3.6 version [duplicate]

I am having trouble using the command line. I have a script test.py (which only contains print("Hello.")), and it is located in the map C:\Python27. In my system variables, I have specified python to be C:\Python27 (I have other versions of Python installed on my computer as well).
I thought this should be enough to run python test.py in the command line, but when I do so I get this:
File "<stdin>", line 1
python test.py
^
SyntaxError: invalid syntax
Looks like your problem is that you are trying to run python test.py from within the Python interpreter, which is why you're seeing that traceback.
Make sure you're out of the interpreter, then run the python test.py command from bash or command prompt or whatever.
Don't type python test.py from inside the Python interpreter. Type it at the command prompt, like so:
You can simply type exit() in the Python terminal to exit the Python interpreter. Then when you run the code, there will be no more errors.
I faced a similar problem, on my Windows computer, please do check that you have set the Environment Variables correctly.
To check that Environment variable is set correctly:
Open cmd.exe
Type Python and press return
(a) If it outputs the version of python then the environment variables are set correctly.
(b) If it outputs "no such program or file name" then your
environment variable are not set correctly.
To set environment variable:
goto Computer-> System Properties-> Advanced System Settings -> Set Environment Variables
Goto path in the system variables; append ;C:\Python27 in the end.
If you have correct variables already set; then you are calling the file inside the python interpreter.
In order to run scripts, you should write the "python test.py" command in the command prompt, and not within the python shell. also, the test.py file should be at the path you run from in the cli.
Running from the command line means running from the terminal or DOS shell. You are running it from Python itself.
Come out of the "python interpreter."
Check out your PATH variable c:\python27
cd and your file location.
3.Now type Python yourfilename.py.
I hope this should work

Categories