Tips for Working with a Python Development Environment #84
kyleburnett
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Install Python and Test Installation
Set Up a Development Environment
Using virtual environments for Python development is considered best practice because it isolates different (and sometimes conflicting) Python projects from each other. This is helpful for package installation tools like
pip. Consider, for instance, two research projects that use the populartqdmpackage. Suppose that project A is an older project and uses version 3.8.0 and project B is using version 4.59.0. The system's Python environment andpippackage manager cannot maintain both versions of thetqdmpackage. Therefore, it would be impossible to run project A and project B simultaneously. By creating a virtual environment for each project, however,pippackage installs will be placed into the virtual environment in which it was executed.Note that virtual environments are considered disposable, so don't commit the directory to source control, and don't add files to the virtual environment folder.
Create a virtual environment
Open a terminal. Make sure the current directory is set to the directory of your project and then execute the following to create your virtual environment.
You should see a new directory called
venvhas been added to your current directory.Activate and deactivate an environment
To activate (i.e., "enter into") a virtual environment, run the following command:
source ./venv/bin/activateTo deactivate a virtual environment, run the following:
Manage dependencies
It's a good idea to keep
pipitself up-to-date, so use the following command when you first create your virtual environment and periodically after that. Note that this should be run inside your virtual environment.You can install other
pippackages with theinstallcommand. The following example installs the latest versions ofproknowandpyclipper:You can ask for a specific version like this:
It's a good idea to freeze a list of your package dependencies and their versions to an output file like this:
pip freeze > requirements.txtThis will create the file
requirements.txtin your current directory, which should be committed to source control. You should run the freeze command anytime you install, upgrade, or uninstall a package. If your virtual environment is destroyed and recreated at some point or you wish to share your project repository with someone else, you can install the complete list of packages in therequirements.txtlike this:Beta Was this translation helpful? Give feedback.
All reactions