python virtual environment .exe file usage - python

What is the difference between running a script by first sourcing:
source /venv/bin/activate
python script.py
and running the script with the actual python exe?
/venv/bin/python script.py
do these two commands always do the same thing? The problem that I am seeing is that if script.py calls other python scripts, and the settings and packages that are in venv don't work.

It depends on how does script.py runs other scripts. If it uses sys.executable, that is the current python, /venv/bin/python then both commands are equivalent.
If script.py runs other scripts using shell (os.system, subprocess, etc) then the first one is the preferred form because it sets $PATH for all subprocesses so that all python scripts use the same virtual environment.
And the final note. If some script(s) being run from script.py have fixed shebang like #!/usr/bin/python those scripts will not be run in your virtual environment regardless of the 1st or 2nd way you run script.py.

Related

How to make the equivallent of .bat for mac and how to install python libraries on mac

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?

How can I run a python script in anaconda environment with another python script? (not by .bat ro .sh file)

I have a python script which I want to run it in an anaconda environment because it uses some library like NumPy and I'm not going to install them.
I can easily write something like this:
# for Windows:
C:\ProgramData\Anaconda3\Scripts\activate.bat && python script.py arg
# for Linux:
source activate base
python script.py arg
but then I should have two scripts. a batch file and a shell script which is not desirable.
I like to have a single solution and independent of OS.
So I thought maybe it's good to have a python script which activates anaconda environment then run my script.(So I can handle OS there). I could do it by subprocess.call(). However, it gives an additional message which it's not desirable and I couldn't solve it even with a StackOverflow question.
Now I'm asking do you have a solution to run a python script in anaconda environment with another python script? (which of course doesn't give a notification like my solution.)

Running commands within virtualenv in python

I am trying to run a python script under python 2 virtualenv. How to run it via batch script/ python script?
I have installed both python2 and python 3 and created virtual env too. I tried it invoking via python script but it didn't even enter the virtualenv. Then i tried the below batch script. But It just executed the first line of the code. i.e just activating the virtual environment. but other lines are not getting executed.
I even tried to execute the 1st line of batch script separately in a bat file and then invoke others using perl/python. but none of them worked.
Please do provide a way to execute these commands either using python script or
a batch file, which I will need to run it via perl/python
The batch file i used :
C:\venv-2\Scripts\activate
pushd <some path>
python test.py
deactivate
Just directly use the virtualenv's Python interpreter:
pushd some_path
c:\venv-2\scripts\python test.py
popd
Not having a Windows environment to hand, this is a non-answer for *nix which might be adapted to Windows.
Instead of calling the venv bin directly, try activating it within a script. For example:
# my-script.sh
# activate
source venv/bin/activate
# this should be in the venv
which python3
python3 -c 'print("Hello from python3")'
Then $ bash my-script.sh should print out which python3 it thinks is in use within the script.
See also A Python script that activates the virtualenv and then runs another Python script? for the same idea done better...
I got the the batch file executed by giving,
C:\venv-2\Scripts\activate & pushd <some path> & python test.py & deactivate
inside the batch file. And used perl script to call the batch file
system('start test.bat')

Running script without virtualenv activation

Is there a difference between running script using virtualenv interpreter (without virtualenv activation) and running it in activated virtualenv?
venv/bin/python some_script.py
vs
source venv/bin/activate
python some_script.py
Running source bin/activate will set the PATH variable to point to your environment bin directory which is useful if you have other command line scripts/binaries installed (this can happen with certain python packages that add shell commands), it will also unset/set PYTHONHOME.
So, if bin/python works for you then you're fine but if some of the packages you're using start behaving strangely (or wrong one gets imported) it's probably because Python is getting the wrong PYTHONHOME or because a certain script is not found in PATH.
If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/python-script.py) then sys.path will automatically be set to use the Python libraries associated with the virtualenv. But, unlike the activation scripts, the environment variables PATH and VIRTUAL_ENV will not be modified. This means that if your Python script uses e.g. subprocess to run another Python script (e.g. via a #!/usr/bin/env python shebang line) the second script may not be executed with the same Python binary as the first nor have the same libraries available to it. To avoid this happening your first script will need to modify the environment variables in the same manner as the activation scripts, before the second script is executed.
source: https://virtualenv.pypa.io/en/16.7.9/userguide.html#activate-script
Yes. Virtualenv creates an interpreter in its own right. Just do this,
which python
For each interpreter, virtualenv and your normal interpreter and see what happens. They will show you two different links to python interpreter. Here's my example:
quazinafiulislam#Nafiuls-Mac: ~/Code/Python/PyTestingZone
$ which python [7:49:26]
/Users/quazinafiulislam/.pyenv/shims/python
quazinafiulislam#Nafiuls-Mac: ~/Code/Python/PyTestingZone
$ source .venv/bin/activate [7:49:29]
(.venv)
quazinafiulislam#Nafiuls-Mac: ~/Code/Python/PyTestingZone
$ which python [7:49:35]
/Users/quazinafiulislam/Code/Python/PyTestingZone/.venv/bin/python

run python script directly from command line

#!/usr/bin/env python
I put that at the top of a script. I've seen that should make the script runnable from the command line without the need for python programname.py. Unless I'm misunderstanding I should be able to use programname.py as long as I have the above line at the top of the script. Is this correct?
It isn't working for me I just get an error indicating that I would have to use python at the beginning of the 'call'.
Universal running of Python scripts
You can pretty much universally run without the shebang (#!) with
python myscript.py
Or nearly equivalently (it places the current directory on your path and executes the module named myscript) (preferably do this!):
python -m myscript
from the command line, as long as you have Python installed and on your path environment variable (i.e. set to run with python, which, if installed, would typically be the case).
Shebangs (#!) are a Unix thing.
The shebang, as you're using it, is typically for running on a Unix platform (typically Apple or Linux). Windows would typically require cygwin to use the shebang.
You can usually default to whatever python is available on your system path with:
#!/usr/bin/env python
Assuming you're on a Unix, you might try other locations for your python setup, like:
#!/usr/bin/python
Muddling through
You can see what python you're currently using by using the unix which command, so if you want to see where your python is coming from, use this command:
which python
or on Windows (cygwin probably can run the shebang):
where python
On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod
chmod +x myscript.py
(chmod also may apply to Cygwin in Windows)
If you're not running as root, you may require sudo, and that would be
sudo chmod +x myscript.py
And then attempt to run (within the same directory) with
./myscript.py
make the file executable
sudo chmod +x /path/to/file.py
and then from the same directory as file.py:
./file.py

Categories