Is there a way to create virtual environment using python code. It would be great help if anyone can help. I need to automate creating virtual environment.As in using subprocesses or something similar
pip3 install pipenv
cd /root_directory_of_project
pipenv shell
sudo apt install python3-pip
pip3 install pipenv
cd /root_directory_of_project
pipenv shell
if you want to see what modules are installed do,
pip3 freeze
do this inside the pipenv shell.
use exit to exit the pipenv shell.
Anytime you want to use the virtual environment just do, pipenv shell in that directory to enter the shell again.
Related
I want to create a make file using which I can automate the virtual environment creation and installation of project dependencies. I've create the following rule:
setup-env:
sudo apt-get install python3 pip
pip install pipenv
pipenv shell
pipenv install
I am able to install python, pipenv and create a virtual environment.
But the last line pipenv install is not getting execute because the control shifts to virtual environment. When I exit from the virtual environment, the install command gets executed.
Is there a way to install dependencies without creating a separate rule?
Thanks!
You can simple install before going into the shell
(pipenv can be installed outside the shell)
change the order of your rules:
setup-env:
sudo apt-get install python3 pip
pip install pipenv
pipenv install
pipenv shell
I have installed virtualenv on my system using http://www.pythonforbeginners.com/basics/how-to-use-python-virtualenv
according to these guidelines, the initial step is:
$ sudo apt-get install python-pip python-dev python-virtualenv
However, I do not want to touch my parent environment. The only reason I believe virtualenv might be of some help for my case is because I have some weird errors that point to python version inconsistencies.
So my requirements are:
virtualenv with e.g. python 3.5
tensorflow
no influence on my parent environment
ability to disable virtualenv with no side effects
Is it doable how?
You could follow the steps in this answer for instance, which will be essentially the same as the guide you've mentioned.
virtualenv installs libraries and all in a subfolder of your main system, and directs python to only use those, so they don't interfere with your main installation.
If you really don't want to touch anything in your system, you could always run tensorflow in a docker container (see this answer for some tips). But even that will require some installation in the "parent" system.
create the env
virtualenv -p python3 path/to/your/env
activate the env
source path/to/your/env/bin/activate
install packages
pip install pkgname
deactivate
deactivate
If you do not want to touch your parent environment, install package using pip after activating the environment. Next time you activate the environment, the installed packages will remain there. If you want to delete the environment, just delete the folder path/to/your/env.
Just run this single command:
It installs python package manager: pip.
It creates a virtual environment named: my_env.
It activates the virtual environment.
sudo apt-get install python3-pip -y && sudo apt install python3.8-venv && python3 -m venv my_env/ && source my_env/bin/activate
Hello Guys I am tying to follow the installation here https://github.com/systers/portal and trying to deploy the server inside a virtual environment on my machine.
After lots of errors I decided to install a fresh copy of Ubuntu 16.04 and start
After the installation here are the things that I have installed using the given commands
I checked my current python and python3 versions using python --version and python3--version respectively and they are Python 2.7.12 and Python 3.5.2 respectively.
Easy Install. $ sudo apt-get install python-setuptools python-dev build-essential
pip. $ sudo easy_install pip
virtualenv. $ sudo pip install --upgrade virtualenv.
python3-dev tools.$sudo apt-get install python3-dev
Now after that I created a virtual env and activated it using the following commands
$ virtualenv venv1 --python=/usr/bin/python3
$ source venv/bin/activate
But now when I run the third command
$ pip install -r requirements/dev.txt
or even do
$pip --version
I get the error
bash: /media/rohan/New Volume/portal/venv1/bin/pip: "/media/rohan/New: bad interpreter: No such file or directory
Also in /venv1/bin the files pip,pip3 ,pip3.5 are present
I tried sudo easy_install pip thinking that it will install pip in the virtual environment but it installs to /usr/local/bin
Also I tried by creating a virtual env using the code
$virtualenv venv --python=/usr/bin/python
But that also doesnt work and this time also same error comes and in /venv/bin pip pip2 pip2.7 are present
PLEASE HELP
The problem appears to be that the path to your virtualenv has a space in it that isn't being escaped somewhere it should be.
Note the error you receive:
/media/rohan/New: bad interpreter: No such file or directory
So with that space in the path, it is trying to run a program that doesn't exist (/media/rohan/New) on a file that doesn't exist (Volume/portal/venv1/bin/pip).
Renaming New Volume to something without spaces like new_volume and then recreating a virtualenv should resolve this.
I had renamed the folder of virtual environment so that I was getting this error.
Then I renamed the venv folder name to the path mentioned in the error (That is the one which I had named while creating venv.) then tried to use pip and it worked.
Might be you have already solved your issue, but this is for the future visitors.
How do you install virtualenv correctly on windows?
I downloaded virtualenv1.9.1 from here and tried installing it with:
python virtualenv.py install
but it does not appear in MyPythonPath/Scripts
I tried the same way installing virutalenvwrapper-win and it installed correctly. But I can't use it because I don't have virtualenv
python.exe: can't open file
'MyPythonPath\Scripts\virtualenv-script.py': [Errno 2 ] No such file or
directory
The suggested way to install Python packages is to use pip
Please follow this documentation to install pip: https://pip.pypa.io/en/latest/installing/
Note: Python 2.7.9 and above, and Python 3.4 and above include pip already.
Then install virtualenv:
pip install virtualenv
Since I got the same error as mentioned in the question inspite of installing with:
pip install virtualenv
I would like to add a few points, that might also help someone else solve the error in a similar way as me. Don't know if that's the best way, but for me nothing else helped.
Install virtualenv
pip install virtualenv
Move into Scripts directory
cd C:\Python27\Scripts
Create a virtual env.
python virtualenv.exe my_env
Activate the virtual env.
my_env\Scripts\activate.bat
Deactivate the virtual env.
my_env\Scripts\deactivate.bat
install virtualenv
pip install virtualenv
create a virtual environment
python -m virtualenv demoEnv
Activate the environment
demoEnv\Scripts\activate
To deactivate
deactivate
There is an other way to install Python packages.
1: download the package, you want
2: open commander (press the win start-button and search for cmd)
3: cd into the folder where you downloaded your package
4: type: "python setup.py install"
For installing virtualenv, you'll have to either install it using pip as mentioned in the answer by woozyking or you'll have to do something like this:
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.9.1.tar.gz
$ tar xvfz virtualenv-1.9.1.tar.gz
$ cd virtualenv-1.9.1
$ [sudo] python setup.py install
The command which you have used can be used to create a virtualenv. I would recommend you go through these small videos on virtualenv and virtualenvwrapper to get a better understanding:
python-power-tools-virtualenv
virtualenvwrapper
Creating a Virtual Environment on Windows
1. Create a virtual environment
python -m venv myenv
2. Activate
.\myenv\Scripts\activate
3. Extra information
To disable write
deactivate
These commands will also work on windows
myenv\Scripts\activate
myenv\Scripts\activate.bat
.\myenv\Scripts\activate.bat
Be careful with slashes:
myenv/Scripts/activate.bat
I prefer using this naming:
python -m venv .venv
.venv\Scripts\activate
4. Screenshot
5. Sources
https://code.visualstudio.com/docs/python/tutorial-django
https://code.visualstudio.com/docs/python/tutorial-flask
In my Ubuntu 12.04 machine, the installation of pip requirements is asking me for sudo permission every time it attempts to install. How would I override this, as this is terrible for my working environment to install things globally instead of inside the venv?
Note: I did not setup the venv using sudo.
Have you activated your virtual environment? Run:
. bin/activate
in your shell. Then the local pip installation will take over the system one.
Thanks to #MartijnPieters, I found a workaround:
Running
~/.virtualenvs/myapp/bin/pip install -r requirements.txt
Instead of just
pip install -r requirements.txt
Make sure you use a recent version of virtualenv itself, the latest at the time of writing is 1.7.2. Old versions required the use of -E switch, to install into the virtual environment.