Unable to run python script in virtualenv using bash script - python

I want to run a python script in in-built anaconda environment tensorflow_p36. To check if it is in virtual environment or not, I am using command pip -V.
My first attempt at bash script:
#!/bin/bash
source activate tensorflow_p36
python /home/ec2-user/abc/temp.py
pip -V
Note: tensorflow_p36 being an in-built environment, does not require to be called from specific /env/bin directory. It can be activated from any directory. I think it's a feature of Amazon Deep Learning AMIs.
My second attempt at bash script:
#!/bin/bash
pythonEnv="/home/ec2-user/anaconda3/envs/tensorflow_p36/"
source ${pythonEnv}bin/activate
${pythonEnv}bin/python /home/ec2-user/abc/temp.py
pip -V
Note: When I try to run source /home/ec2-user/anaconda3/envs/tensorflow_p36/bin/activate command in terminal, the environment isn't being activated.
Each time, I am getting the same result:
pip 9.0.1 from /home/ec2-user/anaconda3/lib/python3.6/site-packages (python 3.6)
Whereas, I should be getting:
pip 9.0.1 from /home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages (python 3.6)
Can someone please explain how do I activate virtual environment and run a python script from that environment? I need to use this particular environment because of the dependencies installed in it.
Extra info:
Not sure if it matters, but the tensorflow_p36 is a conda environment, not a virtualenv.

This works with virtualenv. Create environment:
virtualenv -p python 3.6 tensorflow_p36
Then change the script to:
#!/bin/bash
source $HOME/tensorflow_p36/bin/activate
python /home/ec2-user/abc/temp.py
I believe the confusion has to do with the fact that you are using anaconda and not virtualenv to create a python environment. These two tools work differently.
If you are using an EC2 instance, why not to install tensorflow_p36 globally anyway?

Related

Inplace of python executable inside virtual environment, getting Alias

I was running an old python version of python 3(3.5.2), and because of that, some of the libraries(like the seaborn) were misbehaving. I tried upgrading python to 3.9 with the help of brew and was successfully able to do that. I can now see python inside the /usr/local/bin/python3.9. I also upgraded the pip and virtualenv package. After that, I unlink /usr/local/bin/python and then linked it with /usr/local/bin/python3.9. Next, I set alias in ~/.bash_profile to /usr/local/bin/python.
After performing the above process, python -V gives the new python(ie python 3.9).
But, when I now create the virtualenv with the command -
virtualenv nvenv --python=/usr/local/bin/python
the virtual environment is created but inside nven/bin/ I could see an alias of the python3.9 and not the executable(like I had in my old virtual environment).
I tried with the following commands too but the result was same -
virtualenv nvenv
python -m virtualenv nvenv
I have performed so many steps without really understanding much and now I am confused now where I am going wrong. Could anyone help me to create the virtualenv with my latest python3.9 which also has python3.9 executable inside bin of it? I need it to set interpreter in the spyder IDE.

when should python3 object created in venv?

python newbie here.
I am on a windows machine and used Windows Subsystem for Linux(WSL) to execute commands.
I created my venv(folder) by entering:
python3 -m venv venv
and it would give me a new venv folder as below:
and then activated the virtual environment using:
source venv/bin/activate
however, I am having an issue as I executed another command and it is looking for python3 which should be present as in this path: venv/bin/python3
I am curious how should python3 created in venv. Thanks!
i guess you are confused about the Python virtual environment
first, the virtual environment is designed to establish a command execution environment for each project。
then. the first your code python3 -m venv venv call the system python command and the python3 should in /usr/bin/python3 and linked with /usr/bin/python3.x
but, if you activate the virtual environment of Python, the virtualenv will and the {you_dir}/venv/bin to the system path and you will call python through /venv/bin/python
to execute you python code.
there is no need create an link of /ven/bin/python3. but if you want, you can do it!

How to tell if virtualenv is activated in Windows Git Bash

I created a virtual environment in my new directory with virtualenv env and then in Windows Git Bash ran env/Scripts/activate, which seemed to work. I didn't notice my virtualenv being displayed in parens at the beginning of the line (question1: can I set it up to work like that?), so to check if it was indeed activated I ran pip -V which gave me:
pip 9.0.1 from c:\tools\python2\lib\site-packages (python 2.7)
Shouldn't that be giving the directory of my virtualenv rather than site-packages? I also ran pip list and it gave me a list of installs that I had (naughtily) installed globally for a different project. So I can only assume my virtualenv did not activate and I don't know why that is.
Your suspicions are correct. Try source Scripts/activate. What you did will run the command in a new and temporary shell instance.

awscli running with python 2.7 on OS x Sierra

I'm having an issue with my awscli version + python versions installed on my Mac.
After several tries following this I managed to have it working, however, if i run aws --version I get aws-cli/1.11.170 Python/2.7.10 Darwin/16.7.0 botocore/1.7.28
This confused me because I thought I had just installed python 3.6, so I decided to run which python, and it returned /usr/bin/python
I was still unsure and after some research I found this:
$which python3 /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Now i'm totally unsure which python version is the "main" one and if it is affecting my awscli, i plan to create a restapi using aws SDK with boto3 for Python.
How can i clean my python install or simply make sure my Mac is ready to work with the aws SDK + python?
PS.: I've tried this to uninstall python 2.6, 2.7 and 3.6 (currently installed on my system i suppose) but nothing really happened.
Thanks in advance for the help!
I would recommend creating a virtual environment to create an isolated environment for all the packages in your project. You can create a virtual environment of a specific python version using the following syntax in your shell:
$ python3.6 -m venv env
One you activate the virtual environment, you can use pip to install packages into your local python instance:
$ source env/bin/activate
(env) $ pip install awscli
Whenever you plan to run your application, or install packages for the project, simply activate the virtual environment.
For more information, see the documentation for the venv module in Python's standard library.

Testing a python script in a specific version

I currently have Python 2.6.2 installed on my mac. I am writing a script which MUST run on Python 2.5.2. So I want to write a python script, and test is specifically against 2.5.2 and NOT 2.6.2.
I was looking at virtualenv, but it doesn't seem to solve my problem. I ran python virtualenv.py TEST which made a TEST dir, but it had python 2.6 in it. Is there a way to make virtualenv use a different version of python than what's installed default on my machine? Is another way to use the #! as the first line of the python script? How would I do that?
Check out tox; it's designed to do exactly this.
Here is an example of using tox to run a hello_world.py script against multiple Python versions:
Install tox
pip install tox
Create a tox.ini configuration file
[tox]
envlist = py39, p310, p311
[testenv]
commands = python hello_world.py
The envlist option specifies the Python versions to test against
The commands option specifies the command to run in each environment
Run tox
tox
This will create separate environments for Python 3.9, 3.10, and 3.11 and run the hello_world.py script in each environment.
You can setup a sandboxed environment with different python versions using virtualenv. As Kable has done, install the 2.5. version you want to test against. Then create your virtual environment:
virtualenv --p=python2.5 myapp
To get a clean environment you may use the --no-site-packages switch with the command above. Quite handy when trying to simulate a new, fresh setup. Now activate your virtualenv:
source myapp/bin/activate
If you check the python version you should now get version 2.5.x:
python -V
Now you can install modules as you like into your virtual environment in the usual fashion:
easy_install ...
pip ...
To exit your virtual environment:
deactivate
Hope this may be of help.
You could just install a Python 2.5.2.
I have 3 different versions Python installed on my Lucid and they use different links under /bin/ so it's easy to call the specific version
python -> python3 ->python3.1
python2 -> python2.7
python2.5
try #!/path/to/your/python/version
But make sure you execute the script from the terminal, and make it executable before hand: chmod 755 myscript.py
Using 'virtualenv' you can have different isolated Python environments on a single machine. Also you can switch any-time between the different python interpreter versions.
What is virtualenv?
A Virtual Environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.
How to install?
pip install virtualenv
To create virtual environment for python 2.7 :
root:~# which python2.7
/usr/bin/python2.7
root:~# which python3.4
/usr/local/bin/python3.4
You can also use a Python interpreter of your choice:
root:~# virtualenv -p /usr/bin/python2.7 Vpy27
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /root/Vpy27/bin/python2.7
Also creating executable in /root/Vpy27/bin/python
Installing setuptools, pip, wheel...done.
To begin using the virtual environment, it needs to be activated:
root:~# source Vpy27/bin/activate
The name of the current virtual environment will now appear on the left of the prompt:
(Vpy27) root:~# python -V
Python 2.7.3
Install packages as usual, for example:
(Vpy27) root:~# pip install junos-eznc >> All pip installs done here, will be available only in this environment.
If you are done working in the virtual environment for the moment, you can deactivate it:
(Vpy27) root:~# deactivate
To create virtual environment for python 3.4:
root:~# which python3.4
/usr/local/bin/python3.4
root:~# virtualenv -p /usr/local/bin/python3.4 Vpy34
root:~# source Vpy34/bin/activate
(Vpy34) root:~# python -V
Python 3.4.4
There is also a way to create virtual environment with already available site-packages.

Categories

Resources