Project setup with virtualenv.

While working with a pyhon3 project, it is better to install project based dependencies inside a project wise virtualenv created.

It helps to minimise the interference of different versions of python modules into different projects.

Project setup with virtualenv
# Create a project working directory
$ mkdir /my/path/to/project/dir

# Go to working directoyr
$ cd /my/path/to/project/dir

# Create a virtual env in working directory with name ashuenv using venv module
$  python3 -m venv ashuenv
# In case above command format with specific version, gives an error like,when I tried with 3.10 I got Error: Command '['/home/ashu/work/sb/ashuvenv310/bin/python3.10', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
# Then try 
$ virtualenv -p python3.10 ashuvenv310

# Clone the project in same working directory
$ git clone clone_url
# Now this cloned repo directory and virtualenv directory, sbenv, both are into same working directory.

# To activate virtual envrionment.
$ source ./ashuenv/bin/activate

# Check virtual environment python
$ python --version
$ which python

# To exit activated virtual environment use
$ deactivate

# Go to project repo dir install dependencies
$ cd cloned_dir_name
# Install project dependency with requirement.txt

$ pip install -r requirements.txt

Leave a Reply

Your email address will not be published. Required fields are marked *