Skip to main content

Featured

Designing React States

  Why We Need to Design States We already know that we should use state if we want the component to change the content it is rendering. What we don't have answers for yet is: when should state be called? what type of data should the state be? in which component is it defined? This design process will pay off when trying to answer these questions in complicated projects so practice it with simple projects first.  It follows three main steps: distinguish states from events, distinguish the variable name and data type for the state, distinguish which component should house this state according to the other components that will need it as props  State Design Process Distinguishing State from Event First, we have to figure out what will be a state and what will be an event handler given the three following questions: 1. make a list of actions your user can make and the changes in the UI said actions make  2. generally speaking, user actions are event handlers and the chan...

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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 use pip 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

Popular Posts