杰瑞科技汇

Python workon home是什么?如何配置使用?

Of course! Let's break down what python workon home means and how to make it work.

Python workon home是什么?如何配置使用?-图1
(图片来源网络,侵删)

It looks like you're trying to combine two different commands from Python's ecosystem:

  1. python: The command to run the Python interpreter.
  2. workon home: A command from virtualenvwrapper to activate a specific Python virtual environment named "home".

These two commands don't work together directly. The correct workflow is to first activate the environment and then run Python from within that environment.


The Correct Command (The "How-To")

You need to run these as two separate commands in your terminal.

Step 1: Activate the Virtual Environment

Python workon home是什么?如何配置使用?-图2
(图片来源网络,侵删)

This command loads the "home" environment, making it the active one for your current terminal session.

workon home

You'll know it worked because your command prompt will change to show the name of the active environment, like this:

(home) $

Step 2: Run the Python Interpreter

Now that your "home" environment is active, you can run the Python interpreter. It will automatically use the Python version and any packages you've installed inside this environment.

Python workon home是什么?如何配置使用?-图3
(图片来源网络,侵删)
python

You can also run a Python script directly:

python my_script.py

To exit the Python interpreter, simply type:

exit()

To deactivate the environment and return to your normal shell, type:

deactivate

The "Why" and Common Issues

If workon home didn't work for you, it's likely because virtualenvwrapper isn't installed or configured correctly. Here's a quick guide to get it set up.

What is virtualenvwrapper?

virtualenvwrapper is a set of extensions that make it easier to manage Python virtual environments. Its main commands are:

  • mkvirtualenv <name>: Create a new environment.
  • workon <name>: Activate an existing environment.
  • deactivate: Turn off the environment.
  • rmvirtualenv <name>: Delete an environment.

How to Install and Configure virtualenvwrapper

  1. Install it: It's best to install it into your main Python system, not inside a virtual environment.

    # For Python 3
    pip install virtualenvwrapper
  2. Add to your Shell Configuration: You need to tell your shell (like Bash or Zsh) where to find virtualenvwrapper every time you open a terminal. You do this by adding a few lines to your shell's startup file.

    • For Bash: Edit the ~/.bashrc file.
    • For Zsh: Edit the ~/.zshrc file.

    Add these lines to the end of the file:

    # Virtualenvwrapper settings
    export WORKON_HOME=$HOME/.virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3  # Or your python3 path
    export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv # Or your virtualenv path
    source /usr/local/bin/virtualenvwrapper.sh # This path might vary!
    • WORKON_HOME: This is the directory where your virtual environments will be stored. ~/.virtualenvs is the standard location.
    • VIRTUALENVWRAPPER_PYTHON: Point this to the Python 3 executable you want to use as the base for your environments. You can find its path with which python3.
    • source ...virtualenvwrapper.sh: This is the script that loads the workon, mkvirtualenv, etc., commands. The path is often /usr/local/bin/virtualenvwrapper.sh, but it can be different. Use find /usr -name virtualenvwrapper.sh to locate it if you're unsure.
  3. Apply the Changes: After saving the file, you must either open a new terminal window or run source ~/.bashrc (or source ~/.zshrc) in your current terminal for the changes to take effect.

Now, workon home should work, assuming you have already created an environment named "home" with mkvirtualenv home.


Alternative: Using venv (Built-in to Python)

If you don't want to use virtualenvwrapper, Python 3 comes with its own built-in module called venv which is perfectly fine for managing environments.

  1. Create an environment:

    python3 -m venv ~/.virtualenvs/home
  2. Activate it: The activation command is different for each operating system.

    • On macOS / Linux:

      source ~/.virtualenvs/home/bin/activate
    • On Windows (Command Prompt):

      %USERPROFILE%\.virtualenvs\home\Scripts\activate.bat
    • On Windows (PowerShell):

      %USERPROFILE%\.virtualenvs\home\Scripts\Activate.ps1
  3. Use it: Once activated, your prompt will change, and you can just type python to run the interpreter from that environment.

  4. Deactivate it:

    deactivate

Summary

Action virtualenvwrapper (Recommended) Built-in venv
Create mkvirtualenv home python3 -m venv ~/.virtualenvs/home
Activate workon home source ~/.virtualenvs/home/bin/activate
Run Python python python
Deactivate deactivate deactivate

For most users, virtualenvwrapper is the more convenient and user-friendly option once it's set up.

分享:
扫描分享到社交APP
上一篇
下一篇