Python Virtual Environment Help - Django - python

Error Message
I am struggling to install Django to my new project using pipenv, it keeps telling me to run another virtual environment rather than actually installing Django.
I tried to do what it says to using the other virtual env but it still won't work.

You get that all wrong.
"The venv module supports creating lightweight “virtual environments”, each with their own independent set of Python packages installed in their site directories. A virtual environment is created on top of an existing Python installation, known as the virtual environment’s “base” Python, and may optionally be isolated from the packages in the base environment, so only those explicitly installed in the virtual environment are available.
When used from within a virtual environment, common installation tools such as pip will install Python packages into a virtual environment without needing to be told to do so explicitly."
pip is python package manager and there for tool for installing modules such as Django. If you are running linux you can use following commands
-> cd storefront
-> python -m venv venv (create new virtual environment name: "venv")
-> . venv/bin/activate (to activate virtual environment)
-> pip install django (to install Django modules)

Related

package not being permanently installed inside the python virtual environment

I'm building a rest-api using the Django Python framework. I'm using many external python packages. I have created a python virtual environment (python -m venv venv) and after activating the venv environment (venv\Scripts\activate), I installed the requests package (python -m pip install requests). Then I pushed my project to my git repo and cloned it onto another machine. When I tried to run my Django project, it asked me to install the requests package again. Why or how can I permanently install packages into my python virtual environment or someplace else where I wouldn't have to install packages again on different machines? I'm looking for a solution similar to NodeJS - npm of installing packages as all the packages are locally installed into the node_modules folder of the project and you don't have to reinstall them on different machines. Thanks
The environment itself is not shareable in the way you specify. I'd recommend to use Docker for this use-case. If you create a docker image which has the correct dependencies, then you can easily operate in the same environment on different computers. The python venv cannot be used this way.
Nevertheless, if your requirements.txt files specify package versions, then the venv you create on the two machines should be relatively similar (depending of course on other parameters like the OS, python version, etc.).

I cant get studio code to install dotenv with pip install. Error cant find module, how do I fix it please?

PS C:\Users\gwill\OneDrive\Documents\new luno\Luno10> pip install dotenv
Fatal error in launcher: Unable to create process using '"C:\Users\gwill\AppData\Local\Programs\Python\Python310\python.exe" "C:\Users\gwill\AppData\Local\Programs\Python\Python310\Scripts\pip.exe" install dotenv': The system cannot find the file specified.
It should be pip install python-dotenv.
TL;DR;
In the project folder type this step by step depending on the platform (Linux/macOS or Windows):
Command-Line Linux/macOS
Command-Line Windows
Description
python3 -m venv .venv
python -m venv .venv
Creates the virtualenv (in this casethe .venv folder)
. ./.venv/bin/activate(bash/zsh). ./.venv/bin/activate.fish(fish shell)
.venv\Scripts\activate.bat(cmd.exe).venv\Scripts\Activate.ps1(Powershell)
Activates it
pip install dotenv
pip install dotenv
Installs a 3rd partypackage (in thiscase dotenv)
python script.py
python script.py
Runs the script(in this case script.py)
deactivate
deactivate
Deactivates it later
What is a Virtual Environment?
By default, Python installs 3rd-party packages on the System Site Directories. And in most cases, this even requires admin privileges like sudo (Linux/macOS) or Run as Administrator (Windows).
The System Site Directories is the default environment for the installed Python. It's global for all the projects being worked on on the same system.
So, installing the requirements/3rd-party packages from each project on the System Site Directories is going to pollute it. And worse, it could clash versions of the same module dependency between different projects. And could make projects completely unrunnable.
That's why Virtual Environments exist in the first place. The Virtual Environment is a completely separate Site Directories from the System Site Directories and is (should be) exclusive to the project being developed at the moment. The project itself is going to be totally isolated from other Virtual Environments. And even the Python binaries are going to be copied/sym-linked into the Virtual Environment.
Creating Virtual Environments
Adapted from the official documentation:
Since Python 3.3 it's possible to create a Virtual Environment with the standard venv module that comes preinstalled with Python already (Before, the standard way was to use the virtualenv package).
The creation of Virtual Environments is done by executing the command venv:
Linux/macOS
python3 -m venv .venv
Windows
python -m venv .venv
Running this command creates:
The target directory (.venv in this case);
The .venv/bin folder (or .venv\Scripts on Windows) containing a copy/sym-link of the Python binary/binaries;
The Virtual Site Directories (initially empty): .venv/lib/pythonX.Y/site-packages folder (on Windows, this is .venv\Lib\site-packages).
Activating a Virtual Environment
Once a virtual environment has been created, it can be “activated” using a script in the virtual environment’s binary directory. The invocation of the script is platform-specific:
Platform
Shell
Command to activate the Virtual Environment
POSIX
bash/zsh
source .venv/bin/activate
fish
source <venv>/bin/activate.fish
csh/tcsh
source <venv>/bin/activate.csh
PowerShell Core
.venv/bin/Activate.ps1
Windows
cmd.exe
.venv\Scripts\activate.bat
PowerShell
.venv\Scripts\Activate.ps1
As soon as the Virtual Environment is active, every install with pip is going to install on the Virtual Environment, safe from every other Virtual Environment.
Deactivate a Virtual Environment
You can deactivate a virtual environment by typing deactivate in your shell.

Pip installs packages in pyenv root even when virtual environment is activated

I try to install a package with pip in a virtual environement previously created with venv. The Python version is managed through pyenv. I don't want to use pyenv to create the virtual environment.
The project is set up this way. To the project empty directory, I added a .python-version containing the version 3.8.2. Then I created my virtual environement using python -m venv .venv. Then I activated the environement using source .venv/bin/activate. Now the command line starts with a (.env). However, when I try to install some package with pip install some-package, the package ends up in {pyen_home}/versions/3.8.2/lib/python3.8/site-packages, instead of the virtual environment.
What's irritating is that I'm almost certain that I did manage to install package in the virtual environment that way before, but not anymore, so I don't see what I'm missing.
Content of your .python-version should be .venv.
As far as I know you should not create this file by yourself. It generated when you run pyenv local .venv. And venv activates automatically.
Also proper way to create virtual environment is pyenv virtualenv {python-version} {venv-name}. Read the docs carefully.

How to create a Python virtual environment independent of OS and Python version

I am trying to create a virtual environment to run a script which requires Python 3.6. I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed. I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.
Virtual environments are not portable, they depend on the Python installation you have.
You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.
If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller. It is far from perfect and little bit hacky. Also it generates a package which is specific to operating system.
https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
There is also a detailed step-by-step guide on how to use PyInstaller.
https://realpython.com/pyinstaller-python/
This is step-by-step how I use Python virtual environment and share it with co-workers.
To check python and virtualenv presence, run following commands:
which python3
python3 -m pip list | grep env
which virtualenv
Install a python virtual environment builder:
python3 -m pip install virtualenv
Create a virtual environment named venv inside the project's directory: virtualenv venv
To activate this environment use this command inside project's directory: source venv/bin/activate
Install python modules dependencies listed in a requirements.txt:
python3 -m pip install -r requirements.txt
You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory. When you need to deactivate the virtual environment do it using deactivate command.
To deactivate environment simply run: deactivate

How to setup Django PyDev project with virtualenv created with pyenv

I have set up a python virtual environment with pyenv on Linux Now I would like to create a Django project in PyDev with one of these virtual environments. However, I cannot figure out how to locate my virtual environment, since running which python in the virtual environment only gives me the generic /home/rbu/.pyenv/shims/python.
First find your virtualenv directory with
pyenv prefix <venv-name>
The python executable of the virtualenv should be <path>/<to>/<venv>/bin/python.
Now set up a new interpreter in Eclipse Preferences>PyDev>Interpreters>Python Interpreter using the location of the executable and a adequate name.
After that you can start a new Django Project via File>New>Project>Pydev>PyDev Django Project. Choose your predefined interpreter.
The Django Project should now work inside the virtualenv. For installing new packages it is probably easiest to just activate the virtualenv in the terminal with
pyenv activate <venv-name> and pip install the package.

Categories