The source code for this blog is available on GitHub.
Note
Top

Use Python to Restart a New Project

Cover Image for Use Python to Restart a New Project
Chen Han
Chen Han

Check python and use virtualenv in mac?

You can check the version of Python in your virtual environment on a Mac by activating the virtual environment and running the command python3 -V in the terminal. This will output the version of Python that is currently being used in the virtual environment.

Here are the steps to activate and check the version of Python in a virtual environment on a Mac:

  1. Open the Terminal application on your Mac.
  2. Navigate to the directory where your virtual environment is located using the cd command. For example, if your virtual environment is located in a folder called "my_env" in your home directory, you would use the command cd ~/my_env.
  3. Activate the virtual environment by running the command source bin/activate. You should see the name of your virtual environment in the terminal prompt, indicating that it is active.
  4. Run the command python3 -V to check the version of Python in the virtual environment. This will output the version number, such as Python 3.9.2.
  5. To deactivate the virtual environment, you can use the command deactivate

Alternatively, you can check which python version your virtual environment is currently using by running which python. This will give you the path of python executable in your virtual environment.

See pip list in python

You can view the list of packages installed in your Python environment using the pip list command.

To view the list of packages installed in your current environment, open a terminal or command prompt and enter the command pip list.

Here's an example of the output you might see when running the pip list command:

pip3 list
Package            Version
------------------ ---------
certifi            2022.9.24
charset-normalizer 2.1.1
idna               3.4
instagram-scraper  1.11.0
pip                21.1.1
requests           2.28.1
setuptools         56.0.0
tqdm               4.64.1
urllib3            1.26.12

How to check if pipenv exist in my mac

version in the terminal. If pipenv is installed, this command will output the version number of pipenv, such as pipenv, version 2018.11.26. If pipenv is not installed, you will see an error message indicating that the command is not found.

Here are the steps to check if pipenv is installed on your Mac:

  1. Open the Terminal application on your Mac.
  2. Run the command pipenv --version.
  3. If pipenv is installed, you will see the version number of pipenv, like pipenv, version 2018.11.26.
  4. If pipenv is not installed, you will see an error message indicating that the command is not found, like -bash: pipenv: command not found

If pipenv is not installed, you can install it by running the command pip install pipenv.

Alternatively, you can use which pipenv command to check whether pipenv is installed or not, it will give the path of pipenv executable if it's installed otherwise it will return nothing.

Install pipenv with pip

pip3 install pipenv

Activate pipenv

The command python3 -m pipenv shell is used to activate a virtual environment that is managed by pipenv.

It starts a new shell (command prompt) with the virtual environment activated, allowing you to run commands in the context of the virtual environment. Once the virtual environment is activated, any packages you install or any changes you make to the environment will be isolated from your system Python installation.

python3 -m pipenv shell

✔ Successfully created virtual environment! Virtualenv location: /Users/chenhanting/.local/share/virtualenvs/taiko-web-master-4MDdYgQ4 requirements.txt found in /Users/chenhanting/Downloads/taiko-web-master instead of Pipfile! Converting... Warning: Your Pipfile now contains pinned versions, if your requirements.txt did. We recommend updating your Pipfile to specify the "*" version, instead. Launching subshell in virtual environment...

How to convert a requirements.txt file in the current directory and to a Pipfile.lock file

You can convert a requirements.txt file to a Pipfile.lock file by using the pipenv lock command. This command creates a Pipfile.lock file in the current directory and it will contain the packages and versions specified in the requirements.txt file.

Here are the steps to convert a requirements.txt file to a Pipfile.lock file:

  1. Open a terminal or command prompt and navigate to the directory where your requirements.txt file is located.
  2. Run the command pipenv lock
  3. Pipenv will create a Pipfile.lock file in the current directory and it will contain the packages and versions specified in the requirements.txt file.

It's also important to note that, when you run pipenv lock command, pipenv will install the packages specified in the requirements.txt file in your virtual environment. If you want to install those packages in your system environment, you should use pip install -r requirements.txt instead.

It's also good practice to include the Pipfile.lock file in your project's repository so that others can easily install the necessary dependencies to run your project.

Please let me know if you need any more help with this.

What is requirement.txt for in pyhton. How can i use that

requirements.txt is a file used for specifying the dependencies for a Python project. It lists all the packages, along with their version numbers, that the project requires in order to run properly.

You can use requirements.txt to install all the necessary packages for a project by running the command:

pip install -r requirements.txt

This command will install all the packages listed in the requirements.txt file. You can also create a requirements.txt file for your project by running the command:

pip freeze > requirements.txt

This command will create a requirements.txt file in the current directory and list all the packages, along with their version numbers, that are currently installed in your virtual environment.

It's best practice to include a requirements.txt file in your project's repository so that others can easily install the necessary dependencies to run your project.

I saw server.py and app.py in the project. How to start a server

If you have a server.py or app.py file in your project, it likely means that you have a Python web application that uses a web framework such as Flask or Django. To run the application, you need to activate the virtual environment and run the command to start the server.

Here are the general steps to activate the virtual environment and run the server for a Python web application:

  1. Open a terminal or command prompt and navigate to the directory where your server.py or app.py file is located.
  2. Activate the virtual environment by running the command python3 -m pipenv shell (if you are using pipenv) or source bin/activate (if you are using virtualenv)
  3. Run the command to start the server, for example, python3 server.py or python3 app.py
  4. Once the server starts, you should see a message like "Running on http://localhost:8000" or similar, indicating that the server is running and you can access the application by visiting http://localhost:8000 in your web browser

Please note that the command to start the server may vary depending on the web framework you are using. For example, if you are using Flask, you may need to run the command flask run instead of python3 server.py or python3 app.py.

Also, make sure that your virtual environment is activated, you can check this by looking at

© 2024 WOOTHINK. All Rights Reserved.
Site MapTerms and ConditionsPrivacy PolicyCookie Policy