Featured
Setting up venv for new python project
Both venv and pipenv covered
Why use venv?
Here are some reasons why you should consider using virtual environments for your Python projects:
Isolation: Virtual environments provide an isolated workspace for each project, which means that any changes made to the environment (such as installing or upgrading packages) will not affect other projects or the global Python installation.
Dependency Management: With virtual environments, you can manage dependencies for each project separately. This can help avoid conflicts between different package versions or incompatibilities with other packages.
Reproducibility: Virtual environments allow you to create a reproducible environment for your project, which means that anyone else working on the project can create the same environment and run the code without encountering any issues.
Ease of Use: Virtual environments are easy to create and use. You can create a new virtual environment using the
venv
module that comes with Python, and activate it with a simple command. Once activated, you can usepip
to install packages just as you would in a regular Python environment.
Instructions
1. sudo apt-get install python3-venv
- install the necessary package to make a virtual enviornment
2. mkdir my_project
cd my_project
- cd into your project directory
3. python3 -m venv venv
- create a virtual enviornment called venv
4. source venv/bin/activate
- activate it (should see (venv) in your terminal)
- now any pip installs go to it instead of the python interpreter
5. deactivate
- to turn off your venv once your finished
6. pip freeze > requirements.txt
- this will create said file in your root project directory
- each time you run this command, it will be updated with the packages your venv has
7. pip install -r requirements.txt
- when you download someone else's project from github, this command will install all the packages and version that the original coder's venv had
pipenv
1. install pipenv globally
- pip3 install pipenv
2. make a new project folder and cd into it
3. install a package with said command and venv is automatically created
- pipenv install django
If you want to declare your python version explicitly, use this command to intialize a venv with a specific version before installing anything
- pipenv --python 3.9
This command will initialize a venv at python 3.9
Anyway, now django will be installed in said directory and two new files will be created: Pipfile & Pipfile.lock (will hold python version and package versions for the venv)
Activate the venv with terminal pipenv shell
To end the shell, use the terminal command exit
For any subsequent installs, with the venv activated, use pipenv install package_name
For any packages that are strictly for development only, use the --dev flag with pipenv install --dev package_name
When you are done, lock the dependencies with pipenv lock which will record all the packages and their version numbers.
Now when you push this repo to your production server, install everything exactly as it is in those two files with pipenv install --ignore-pipfile (will ignore packages that were installed with dev flag)
If you need to continue development, install dev dependencies as well with pipenv install --dev
For more on pipenv: https://realpython.com/pipenv-guide/
Comments
Post a Comment