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
Related
I have set up a virtual environment correctly have activated it and when I do "which python" it tells me the correct directory, but when I do pip install in the venv it installs the package in the default directory of the mac and not in my venv. I have tried using pycharm and installing packages with that, but it happens the same thing.
Edit:
Following I will do a walkthru of my steps, first I did python3 -m venv /path/to/new/virtual/environment, then I did source env/bin/activate, then I did which python and I got the intended directory, afterwards I did pip3 install numpy and I saw the installation process, then I did pip list and numpy was not there, I checked the directory manually and it still was not there. I retried all the same thing with pycharm with the same results.
Follow these steps in order -
In your current working directory :
python3 -m venv venv
source venv/bin/activate
(venv) pip3 install library_name
To check if libraries were properly installed or not :
(venv) pip3 freeze
and check if your installed libraries is displayed on the terminal screen or not.
To deactivate the virtual environment :
(venv) deactivate
This is due to permission issues
Run this command to change permission of your virtual environment folder
sudo chmod -R 777 venv
After activating your virtual environment
source venv/bin/activate
Then try to install Django by either Python3 or Python2
pip3 install django or pip install django
Then check the list of installed packages by either Python3 or Python2
pip3 list or pip list
NOTE: If you run sudo pip3 install django or pip install in virtual environment that is not writeable by default it will be installed outside of virtual environment
If you run pip3 install django or pip install in virtual environment that is not writtable by default it will be installed outside of virtual environment you will get OS Persmission Error
I have used pip3 and pip because you can install packages by either Python3 or Python2, thing to remember if you created virtual environment by Python3, use pip3 and if you created by using Python2 use pip.
I'm using Linux Mint 20.2 with two directories / and /home.
I used the following command to install virtualenv:
>>> sudo pip3 install virtualenv
It worked fine and it installed in the following path:
>>> virtualenv --version
virtualenv 20.0.17 from /usr/lib/python3/dist-packages/virtualenv/__init__.py
But when I tried to create an environment I got the following error:
>>> python3 -m venv article
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/username/article_tools/article/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
When I tried to uninstall it to install it using [b]sudo apt install python3.8-venv[/b], I got the following error:
>>> sudo pip3 uninstall virtualenv
Found existing installation: virtualenv 20.0.17
Not uninstalling virtualenv at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'virtualenv'. No files were found to uninstall.
How can I fix it? By fix, I mean installing virtualenv in a way that I don't get such errors.
The fundamental problem here seems to be that you are mixing up two different packages.
Python 3 comes with a built-in virtual environment module venv which is however not installed by default on Debian-based platforms. Like the error message says, apt-get install -y python3-venv will install this package, which you can then use with python3 -m venv.
virtualenv is a separate third-party package which you invoke with the command virtualenv. It's not a bad alternative, but if you are only just learning, I would suggest you simply ignore it for the time being.
I had installed virtualenv by sudo -H pip3 install virtualenv, and created a venv by virtualenv, but now I can't create env in my new Django project that includes Pipfile.
I tried:
$ pipenv shell
But its result is:
Creating a virtualenv for this project...
Pipfile: /home/mostafa/py38/lib/python3.8/site-packages/carfix/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv...
⠹ Creating virtual environment...ModuleNotFoundError: No module named 'virtualenv.seed.via_app_data'
✘ Failed creating virtual environment
I tried these answers but the problem is not yet resolved:
Answer 1
I finally found the solution to the problem:
I Used poetry lockfor one app and everything worked fine!
I Used poetry lock for a second app and received the following error message:
$ poetry lock
Creating virtualenv mytestapp-vm7OCEgV-py3.8 in /home/alexb7217/.cache/pypoetry/virtualenvs
ModuleNotFoundError
No module named 'virtualenv.seed.via_app_data'
at <frozen importlib._bootstrap>:973 in _find_and_load_unlocked
Followed the most simple suggestion:
$ sudo apt remove --purge python3-virtualenv
Re-ran poetry-lock
$ poetry lock
Updating dependenciesResolving dependencies... (1.1s)
Everything works great, that's it! ;)
The solution is to remove the installed virtualenv using
pip3 uninstall virtualenv
And use the default installation of virtualenv then :
pipenv shell
Try to remove your virtualenv installation and try to install it via:
sudo apt-get install virtualenv.
Then create virtualenv with:
virtualenv virtualenv_dir
Where virtualenv_dir is your Django project folder
I had the same issue. I was not able to select an existing environment nor create one.
What I did was to create one from the terminal:
virtualenv venv
get into it
. venv/bin/activate
install the requirements I had, with pip3 no pip:
pip3 install -r requirements.txt
and when I went back to pycharm, it asked me if I wanted to use that environment I just created.
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.
When I try python3 -m venv venv, terminal returns
python3: posix_spawn: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.82.7:No such file or directory
I have seen other posts mentioning Home Brew related issues but I doubt this is the case. I did try uninstalling and installing Python.
Note: I was connected to localhost with a virtualenv. When I tried again later on, this was returned. I am building a Flask application.
Now you can use pipenv library for creating the virtual environment.
pipenv install pipenv
for example:
mkdir testapp
cd testapp
pipenv shell
Now the virtual environment is ready.
Then to install libraries in the virtual environment instead of "pip" you have to use "pipenv".
For example:
pipenv install requests
pipenv install flask
Then to write the requirements.txt you can use
pip freeze > requirements.txt
To uninstall libraries:
pipenv uninstall requests
Each time to work on the virtual environment:
pipenv shell
How to exit the virtual environment?
exit